|
|
@@ -14,7 +14,6 @@ namespace ClassFile
|
|
|
|
|
|
Thread threadWatch = null; //负责监听客户端的线程
|
|
|
Socket socketWatch = null; //负责监听客户端的套接字
|
|
|
- Socket socConnection;
|
|
|
|
|
|
public bool IsOpen
|
|
|
{
|
|
|
@@ -99,26 +98,68 @@ namespace ClassFile
|
|
|
|
|
|
private void WatchConnecting()
|
|
|
{
|
|
|
+ Socket connection = null;
|
|
|
+ while (true) //持续不断监听客户端发来的请求
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ connection = socketWatch.Accept();
|
|
|
+ }
|
|
|
+ catch (Exception)
|
|
|
+ {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ //获取客户端的IP和端口号
|
|
|
+ IPAddress clientIP = (connection.RemoteEndPoint as IPEndPoint).Address;
|
|
|
+ int clientPort = (connection.RemoteEndPoint as IPEndPoint).Port;
|
|
|
+
|
|
|
+ //让客户显示"连接成功的"的信息
|
|
|
+ string sendmsg = "连接服务端成功!\r\n" + "本地IP:" + clientIP + ",本地端口" + clientPort.ToString();
|
|
|
+ byte[] arrSendMsg = Encoding.UTF8.GetBytes(sendmsg);
|
|
|
+ connection.Send(arrSendMsg);
|
|
|
+
|
|
|
+ //客户端网络结点号
|
|
|
+ string remoteEndPoint = connection.RemoteEndPoint.ToString();
|
|
|
+ //显示与客户端连接情况
|
|
|
+ richtext.AppendText("成功与" + remoteEndPoint + "客户端建立连接!\t\n");
|
|
|
+
|
|
|
+ //IPEndPoint netpoint = new IPEndPoint(clientIP,clientPort);
|
|
|
+ IPEndPoint netpoint = connection.RemoteEndPoint as IPEndPoint;
|
|
|
+
|
|
|
+ //创建一个通信线程
|
|
|
+ ParameterizedThreadStart pts = new ParameterizedThreadStart(recv);
|
|
|
+ Thread thread = new Thread(pts);
|
|
|
+ //设置为后台线程,随着主线程退出而退出
|
|
|
+ thread.IsBackground = true;
|
|
|
+ //启动线程
|
|
|
+ thread.Start(connection);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ void recv(object socketclientpara)
|
|
|
+ {
|
|
|
+ Socket socketServer = socketclientpara as Socket;
|
|
|
while (true)
|
|
|
{
|
|
|
- socConnection = socketWatch.Accept();
|
|
|
- while (true) //持续不断监听客户端发来的请求
|
|
|
+ //创建一个内存缓冲区,其大小为1024*1024字节 即1M
|
|
|
+ byte[] arrServerRecMsg = new byte[1024 * 1024];
|
|
|
+ //将接收到的信息存入到内存缓冲区,并返回其字节数组的长度
|
|
|
+ try
|
|
|
{
|
|
|
- byte[] data = new byte[1024];
|
|
|
- socConnection.Receive(data);
|
|
|
- if (data[0] == 0)
|
|
|
- {
|
|
|
- break;
|
|
|
- }
|
|
|
- int length = data[5];
|
|
|
- byte[] datashow = new byte[length + 6];
|
|
|
- for (int i = 0; i <= length + 5; i++)
|
|
|
- {
|
|
|
- datashow[i] = data[i];
|
|
|
- }
|
|
|
- string str = Encoding.ASCII.GetString(datashow);
|
|
|
- richtext.AppendText("来自" + socConnection.RemoteEndPoint.ToString() + "的消息:" + str + "\n");
|
|
|
- LogManager.DoLog(str);
|
|
|
+ int length = socketServer.Receive(arrServerRecMsg);
|
|
|
+ //将机器接受到的字节数组转换为人可以读懂的字符串
|
|
|
+ string strSRecMsg = Encoding.UTF8.GetString(arrServerRecMsg, 0, length);
|
|
|
+ //将发送的字符串信息附加到文本框txtMsg上
|
|
|
+ richtext.AppendText("客户端:" + socketServer.RemoteEndPoint + ",\r\n" + strSRecMsg + "\r\n\n");
|
|
|
+ socketServer.Send(Encoding.UTF8.GetBytes("ReturnData"));
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ //提示套接字监听异常
|
|
|
+ richtext.AppendText("客户端" + socketServer.RemoteEndPoint + "已经中断连接" + "\r\n" + ex.Message + "\r\n" + ex.StackTrace + "\r\n");
|
|
|
+ //关闭之前accept出来的和客户端进行通信的套接字
|
|
|
+ socketServer.Close();
|
|
|
+ break;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -132,6 +173,7 @@ namespace ClassFile
|
|
|
|
|
|
public void SendOrder(string Code)
|
|
|
{
|
|
|
+
|
|
|
switch (Code)
|
|
|
{
|
|
|
default:
|