我用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);
}
}
}
解决方案 »
- 利用 httpmodule 处理静态页面问题
- Rational Rose可以对C#进行反向工程吗?
- datalist的一个问题
- ajax传值到handler出错
- 如何知道访问我之前的页面(Request从何处而来)?
- 文件下载后树不能用了~~~~~~```
- 怎么在asp.net中做一个颜色选取的按钮
- 问一下,在web页面下,能显示MessageBox么?
- 很头疼的问题. page_unload 事件到底触发几次? 我想调用数据库组件的 dispose() 方法, 应该放在什么地方?
- 一个看起来很简单的问题……
- |M| 刚才我的一个问题和图片路径有关 慕白兄 说不要用绝对路径 有点不明 是不是aspx用想对路径 ascx用绝对路径
- MasterPage引用Javascript路径错误的问题
WINFROM用多线程处理这种情况,当你接收时就是等待发送方的,如果单线程(WINFORM,控制台没问题),会选成程序无响应一样,可能会导制程序阻塞,无法接收,猜测二。