}
catch (Exception ex)
{
hander.Close();
hander.Dispose();
}
catch就不用说了吧 客户端在我设置的时间内没有返回肯定会抛异常
catch (Exception ex)
{
hander.Close();
hander.Dispose();
}
catch就不用说了吧 客户端在我设置的时间内没有返回肯定会抛异常
我也是最近发现这问题 socket以前做的还是比较多 也没发现
断开-连接-断开-连接……挺灵的
http://technet.microsoft.com/zh-cn/library/wahsac9k(v=vs.100).aspx
你是怎么判断它已经断开连接的 在360中显示么?还是
刚写了一个简单的测试程序
服务器端
private void button1_Click(object sender, EventArgs e)
{ new Thread((ThreadStart)delegate {recive(); }).Start();
}
void recive()
{ //接收信息
IPAddress ips = Dns.GetHostAddresses(Dns.GetHostName())[0];
IPEndPoint ip = new IPEndPoint(IPAddress.Any, 5050);
Socket soc = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
soc.Bind(ip);
soc.Listen(10);
Socket hander = soc.Accept();
hander.ReceiveTimeout = 2000;
try
{
while (true)
{
byte[] bytes = new byte[1024];
int len = hander.Receive(bytes);
hander.Send(new byte[] { 0x3A, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3B });
Thread.Sleep(1000);
}
}
catch
{
hander.Close();
hander.Dispose();
}
}
客户端
private void button1_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
td = new Thread(new ThreadStart(send));
td.IsBackground = true;
td.Start(); }
byte[] bt = new byte[1024];
Socket senSock = null;
void send()
{ senSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
IPEndPoint sendEndPoint = new IPEndPoint(IPAddress.Parse("192.168.2.128"), 5050);
senSock.SendTimeout = 2000;
senSock.Connect(sendEndPoint);
senSock.Send(new byte[] { 0x3A, 0x03, 0x01, 0x01, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3B });
while (true)
{ int len = senSock.Receive(bt);
senSock.Send(new byte[] { 0x3A, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3B });
Thread.Sleep(1000);
}
}
catch
{
senSock.Close();
}
}