网络编程问题
private void Form1_Load(object sender, EventArgs e)
{
Thread myThread = new Thread(new ThreadStart(AcceptConnection));
myThread.Start();
}
private void AcceptConnection()
{
IPAddress[] ip = Dns.GetHostAddresses(Dns.GetHostName());
listener = new TcpListener(ip[0], 23432);
listener.Start();
while (isExit == false)
{
try
{
allDone.Reset();
AsyncCallback callback = new AsyncCallback(AcceptTcpClientCallback);
this.toolStripStatusLabel1.Text= "开始等待连接……";
listener.BeginAcceptTcpClient(callback, listener);
allDone.WaitOne();
}
catch (Exception e)
{
string s = "da";
this.toolStripStatusLabel1.Text = e.Message;
break;
}
}
}
private void AcceptTcpClientCallback(IAsyncResult iar)
{
try
{
allDone.Set();
TcpListener mylistener = (TcpListener)iar.AsyncState;
TcpClient client = mylistener.EndAcceptTcpClient(iar);
this.toolStripStatusLabel1.Text = "已接受客户连接:" + client.Client.RemoteEndPoint;
comboBox1.Invoke(setcomboxcallback, client.Client.RemoteEndPoint.ToString());
DataReadWrite datareadwrite = new DataReadWrite(client);
clientlist.Add(datareadwrite);
//SendString(datareadwrite, "服务器已经接受连接,请通话……");
datareadwrite.ns.BeginRead(datareadwrite.read, 0, datareadwrite.read.Length, ReadCallBack, datareadwrite);
}
catch (Exception e)
{
this.toolStripStatusLabel1.Text = e.Message;
return;
}
}
private void ReadCallBack(IAsyncResult iar)
{
DataReadWrite datareadwrite = (DataReadWrite)iar.AsyncState;
try
{
int recv = datareadwrite.ns.EndRead(iar);
if (recv == 0)
{
this.toolStripStatusLabel1.Text = string.Format("{0}断开连接", datareadwrite.client.Client.RemoteEndPoint.ToString());
this.comboBox1.Invoke(removecomboboxitemscallback, datareadwrite.client.Client.RemoteEndPoint.ToString());
datareadwrite.client.Client.Close();
return;
}
//this.listBoxMsg.Invoke(setlistboxmsgcallback, string.Format("[来自{0}]{1},{2}个字节", datareadwrite.client.Client.RemoteEndPoint, CommandClass.StrHelp(msg).Substring(0, recv * 2), recv));
if (isExit == false)
{
datareadwrite.InitReadArray();
datareadwrite.ns.BeginRead(datareadwrite.read, 0, datareadwrite.read.Length, ReadCallBack, datareadwrite);
}
}
catch (Exception e)
{
this.toolStripStatusLabel1.Text = e.Message;
}
}
客户端能连接上收发信息没问题,但如果客户端断开连接后再重新连接,能连接上,可是一发信息就出问题了:“无法将数据写入传输连接,无法访问已释放的对象,对象名:system.Net.Sockets.Socket”,给出解决的代码,谢谢,没搞过网络编程,很多都不懂,这里很多代码都是抄别人的。
{
Thread myThread = new Thread(new ThreadStart(AcceptConnection));
myThread.Start();
}
private void AcceptConnection()
{
IPAddress[] ip = Dns.GetHostAddresses(Dns.GetHostName());
listener = new TcpListener(ip[0], 23432);
listener.Start();
while (isExit == false)
{
try
{
allDone.Reset();
AsyncCallback callback = new AsyncCallback(AcceptTcpClientCallback);
this.toolStripStatusLabel1.Text= "开始等待连接……";
listener.BeginAcceptTcpClient(callback, listener);
allDone.WaitOne();
}
catch (Exception e)
{
string s = "da";
this.toolStripStatusLabel1.Text = e.Message;
break;
}
}
}
private void AcceptTcpClientCallback(IAsyncResult iar)
{
try
{
allDone.Set();
TcpListener mylistener = (TcpListener)iar.AsyncState;
TcpClient client = mylistener.EndAcceptTcpClient(iar);
this.toolStripStatusLabel1.Text = "已接受客户连接:" + client.Client.RemoteEndPoint;
comboBox1.Invoke(setcomboxcallback, client.Client.RemoteEndPoint.ToString());
DataReadWrite datareadwrite = new DataReadWrite(client);
clientlist.Add(datareadwrite);
//SendString(datareadwrite, "服务器已经接受连接,请通话……");
datareadwrite.ns.BeginRead(datareadwrite.read, 0, datareadwrite.read.Length, ReadCallBack, datareadwrite);
}
catch (Exception e)
{
this.toolStripStatusLabel1.Text = e.Message;
return;
}
}
private void ReadCallBack(IAsyncResult iar)
{
DataReadWrite datareadwrite = (DataReadWrite)iar.AsyncState;
try
{
int recv = datareadwrite.ns.EndRead(iar);
if (recv == 0)
{
this.toolStripStatusLabel1.Text = string.Format("{0}断开连接", datareadwrite.client.Client.RemoteEndPoint.ToString());
this.comboBox1.Invoke(removecomboboxitemscallback, datareadwrite.client.Client.RemoteEndPoint.ToString());
datareadwrite.client.Client.Close();
return;
}
//this.listBoxMsg.Invoke(setlistboxmsgcallback, string.Format("[来自{0}]{1},{2}个字节", datareadwrite.client.Client.RemoteEndPoint, CommandClass.StrHelp(msg).Substring(0, recv * 2), recv));
if (isExit == false)
{
datareadwrite.InitReadArray();
datareadwrite.ns.BeginRead(datareadwrite.read, 0, datareadwrite.read.Length, ReadCallBack, datareadwrite);
}
}
catch (Exception e)
{
this.toolStripStatusLabel1.Text = e.Message;
}
}
客户端能连接上收发信息没问题,但如果客户端断开连接后再重新连接,能连接上,可是一发信息就出问题了:“无法将数据写入传输连接,无法访问已释放的对象,对象名:system.Net.Sockets.Socket”,给出解决的代码,谢谢,没搞过网络编程,很多都不懂,这里很多代码都是抄别人的。
...全文
相关推荐
python socket网络编程之粘包问题详解
一,粘包问题详情 1,只有TCP有粘包现象,UDP永远不会粘包 你的程序实际上无权直接操作网卡的,你操作网卡都是通过操作系统给用户程序暴露出来的接口,那每次你的程序要给远程发数据时,其实是先把数据从用户态copy到内核态,这样的操作是耗资源和时间的,频繁的在内核态和用户态之前交换数据势必会导致发送效率降低, 因此socket 为提高传输效率,发送方往往要收集到足够多的数据后才发送一次数据给对方。若连续几次需要send的数据都很少,通常TCP socket 会根据优化算法把这些数据合成一个TCP段后一次发送出去,这样接收方就收到了粘包数据。 2,首先需要掌握一个socket收发消息的原理 发
c#网络编程处理网络粘包问题
c# 网络编程处理网络粘包的代码,有服务器的以及客户端的详细代码
呵呵关于网络编程的一些棘手问题
呵呵关于网络编程的一些棘手问题,关于网站登录和修改密码的后台程序
JAVA网络编程常见问题与答案
TCP连接和断开与哪些SOCKET语句有关
Linux网络编程:绑定( bind )需要注意的问题
Linux网络编程:绑定( bind )需要注意的问题, 相关教程链接如下: http://blog.csdn.net/tennysonsky/article/details/44057585



