110,566
社区成员
发帖
与我相关
我的任务
分享
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
{
return tcpClient.GetStream().Read(buffer, offset, size);
}
catch
{
tcpClient.Close();
return 0;
}
}
private bool WriteByte(byte[] buffer, int offset, int size)
{
try
{
tcpClient.GetStream().Write(buffer, offset, size);
return true;
}
catch
{
tcpClient.Close();
return false;
}
}
private bool 接收定长数据(byte[] buff, int offset, int needLen)
{
int recvLen = 0;
while (true)
{
int len = ReadByte(buff, offset + recvLen, needLen - recvLen);
if (len <= 0) return false;
recvLen += len;
if (recvLen == needLen) return true;
}
}
private void Run()
{
const int sendLen = 100;
const int readLen = 10;
var readBuff = new byte[readLen + 4];
var sendBuff = new byte[sendLen + 4];
var array = BitConverter.GetBytes(sendLen);
for (int i = 0; i < 4; i++) sendBuff[i] = array[i];
while (tcpClient.Connected)
{
Thread.Sleep(0);
if (!接收定长数据(readBuff, 0, 4) || // 接收4个字节报文头
BitConverter.ToInt32(readBuff, 0) != readLen || // 报文头有误
!接收定长数据(readBuff, 4, readLen)) // 接收报文内容
{
tcpClient.Close();
continue;
}
WriteByte(sendBuff, 0, sendBuff.Length);
}
监听线程.ClientCount--;
}
}
partial class 客户端通信线程
{
private static List<客户端通信线程> list = new List<客户端通信线程>();
private static string Ip = "";
private static int Port = 0;
public static void 启动客户端线程(string strIp, int port, int maxCount)
{
Ip = strIp;
Port = port;
for (int i = 0; i < maxCount; i++)
{
var obj = new 客户端通信线程();
obj.启动通信线程();
list.Add(obj);
}
}
public static int 获取正常连接数()
{
int count = 0;
foreach (var item in list)
{
if (item.Active) count++;
}
return count;
}
}
public partial class 客户端通信线程
{
private TcpClient tcpClient = new TcpClient();
public bool Active = false;
public void 启动通信线程()
{
this.tcpClient.ReceiveTimeout = 2000;
this.tcpClient.SendTimeout = 2000;
var th = new Thread(Run);
th.IsBackground = true;
th.Start();
}
private bool Connect()
{
try
{
tcpClient = new TcpClient();
tcpClient.Connect(Ip, Port);
return tcpClient.Connected;
}
catch
{
return false;
}
}
// 下面的3个方法与服务端完全一致,不重复贴出
private int ReadByte(byte[] buffer, int offset, int size)
private bool WriteByte(byte[] buffer, int offset, int size)
private bool 接收定长数据(byte[] buff, int offset, int needLen)
private void Run()
{
const int sendLen = 10;
const int readLen = 100;
var readBuff = new byte[readLen + 4];
var sendBuff = new byte[sendLen + 4];
var array = BitConverter.GetBytes(sendLen);
for (int i = 0; i < 4; i++) sendBuff[i] = array[i];
while (true)
{
Thread.Sleep(50);
this.Active = tcpClient.Connected;//用于界面显示连接数
if (!tcpClient.Connected && !Connect())
{
Thread.Sleep(500);
continue;
}
for (int i = 0; i < 20; i++)
{// 连续通信多次
if (!tcpClient.Connected) break;
WriteByte(sendBuff, 0, sendBuff.Length);
if (!接收定长数据(readBuff, 0, 4) || // 接收4个字节报文头
BitConverter.ToInt32(readBuff, 0) != readLen || // 报文头有误
!接收定长数据(readBuff, 4, readLen)) // 接收报文内容
{
tcpClient.Close();
continue;
}
}
}
}
}
//主线程异常
Application.ThreadException +=
new ThreadExceptionEventHandler(Application_ThreadException);
//其他线程异常
AppDomain.CurrentDomain.UnhandledException +=
new UnhandledExceptionEventHandler(AppDomain_UnhandledException);
private static void AppDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{// 各种未知线程异常
Log("程序线程中出现未知异常,信息如下:\n" + ((Exception)e.ExceptionObject).ToString());
Environment.Exit(800);
}
private static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
{// 主线程未知异常
Log("程序主线程中出现未知异常,信息如下:\n" + e.Exception.ToString());
Environment.Exit(900);
}