日期:2014-05-17 浏览次数:21124 次
public class 监听线程
{
public static int ClientCount = 0;
private static List<服务端通信线程> list = new List<服务端通信线程>();
private static string Ip = "";
private static int Port = 0;
public static void 启动监听线程(string strIp, int port)
{
Ip = strIp;
Port = port;
var th = new Thread(Run);
th.IsBackground = true;
th.Start();
}
private static void Run()
{
var tcpListener = new TcpListener(IPAddress.Parse(Ip), Port);
tcpListener.Start();
while (true)
{
Thread.Sleep(100);
var tcpClient = tcpListener.AcceptTcpClient();
var obj = new 服务端通信线程();
obj.启动通信线程(tcpClient);
list.Add(obj);
ClientCount++;
}
}
}
public class 服务端通信线程
{
private TcpClient tcpClient = null;
public void 启动通信线程(TcpClient tcpClient)
{
this.tcpClient = tcpClient;
this.tcpClient.ReceiveTimeout = 2000;
this.tcpClient.SendTimeout = 2000;
var th = new Thread(Run);
th.IsBackground = true;
th.Start();
}
private int ReadByte(byte[] buffer, int offset, int size)
{
try
{