我用SOCKET接收文件,为什么会出现着错误:由于系统缓冲区空间不足或列队已满,不能执行套接字上的操作
程序下,
发送方:using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.IO;
namespace send
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//发送
private void button1_Click(object sender, EventArgs e)
{ Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Blocking = true;
System.Net.IPEndPoint ipendpiont = new System.Net.IPEndPoint(System.Net.IPAddress.Parse("192.168.31.250"), 8080);
FileStream fs = new FileStream(@"d:\aa.txt", FileMode.OpenOrCreate, FileAccess.Read);
byte[] bt = new byte[fs.Length - 1];
BinaryReader br = new BinaryReader(fs);
br.Read(bt, 0, Convert.ToInt32(fs.Length) - 1);
socket.Connect(ipendpiont); //连接远程计算机
socket.Send(bt);
fs.Close();
socket.Shutdown(SocketShutdown.Send);//'关闭发送连接
socket.Close(); //关闭本机socket
}
}
}
接收方:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.IO;
namespace recv
{
public partial class Form1 : Form
{
Socket resocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
byte[] bb = new byte[22988899000];
Socket hostsock = resocket.Accept(); //同意和发送端计算机建立连接
hostsock.Receive(bb);
if (hostsock.Receive(bb,bb.Length,0) > 0.1)
{ MessageBox.Show("yyyyy" + hostsock.Receive(bb));
}
FileStream refs = new FileStream(@"c:\aa.txt", FileMode.OpenOrCreate);
BinaryWriter bw = new BinaryWriter(refs);
bw.Write(bb, 0, bb.Length - 1);
refs.Close();
hostsock.Shutdown(SocketShutdown.Receive);
hostsock.Close();
} private void Form1_Load(object sender, EventArgs e)
{
System.Net.IPEndPoint hostip = new System.Net.IPEndPoint(System.Net.IPAddress.Parse("192.168.31.250"), 8080);
resocket.Bind(hostip);
resocket.Listen(10000);
}
}
}
程序下,
发送方:using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.IO;
namespace send
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//发送
private void button1_Click(object sender, EventArgs e)
{ Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Blocking = true;
System.Net.IPEndPoint ipendpiont = new System.Net.IPEndPoint(System.Net.IPAddress.Parse("192.168.31.250"), 8080);
FileStream fs = new FileStream(@"d:\aa.txt", FileMode.OpenOrCreate, FileAccess.Read);
byte[] bt = new byte[fs.Length - 1];
BinaryReader br = new BinaryReader(fs);
br.Read(bt, 0, Convert.ToInt32(fs.Length) - 1);
socket.Connect(ipendpiont); //连接远程计算机
socket.Send(bt);
fs.Close();
socket.Shutdown(SocketShutdown.Send);//'关闭发送连接
socket.Close(); //关闭本机socket
}
}
}
接收方:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.IO;
namespace recv
{
public partial class Form1 : Form
{
Socket resocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
byte[] bb = new byte[22988899000];
Socket hostsock = resocket.Accept(); //同意和发送端计算机建立连接
hostsock.Receive(bb);
if (hostsock.Receive(bb,bb.Length,0) > 0.1)
{ MessageBox.Show("yyyyy" + hostsock.Receive(bb));
}
FileStream refs = new FileStream(@"c:\aa.txt", FileMode.OpenOrCreate);
BinaryWriter bw = new BinaryWriter(refs);
bw.Write(bb, 0, bb.Length - 1);
refs.Close();
hostsock.Shutdown(SocketShutdown.Receive);
hostsock.Close();
} private void Form1_Load(object sender, EventArgs e)
{
System.Net.IPEndPoint hostip = new System.Net.IPEndPoint(System.Net.IPAddress.Parse("192.168.31.250"), 8080);
resocket.Bind(hostip);
resocket.Listen(10000);
}
}
}
解决方案 »
- VS2010的WebApplication项目无法添加项
- DataGird动态创建列的问题
- 表格和层滚动条问题
- 如何在asp.net里恢复和备份数据库
- 控件开发高难度,
- 请问aspx页面的乱码问题如何解决?
- 为什么将page指令中的AutoEventWireup设置为false后,Page_load事件就不会执行了?why?
- .net程序发布为什么在win2003中不行啊!!!!
- 问一个恶心我好几个小时的数据库问题
- mysql汉字存储设置问题
- |M| 刚才我的一个问题和图片路径有关 慕白兄 说不要用绝对路径 有点不明 是不是aspx用想对路径 ascx用绝对路径
- MasterPage引用Javascript路径错误的问题
WINFROM用多线程处理这种情况,当你接收时就是等待发送方的,如果单线程(WINFORM,控制台没问题),会选成程序无响应一样,可能会导制程序阻塞,无法接收,猜测二。