1
2
3
4
5 |
public
static class TcpClientEx { public
static bool IsOnline( this
TcpClient c) { return
!((c.Client.Poll(1000, SelectMode.SelectRead) && (c.Client.Available == 0)) || !c.Client.Connected); } } |
1
2
3
4
5
6
7
8
9
10 |
var conn=state as
TcpClient; while (conn.IsOnline()){ //当网络连接未中断时循环 using
( var
s = conn.GetStream()){ var
buff= new
byte [512]; if (s.DataAvailable){ //判断有数据再读,否则Read会阻塞线程。后面的业务逻辑无法处理 var
len = s.Read(buff,0,buff.Length); } //略... } } |
原文:http://www.cnblogs.com/Lexy/p/3556924.html