public bool Connect() //连接服务器 { clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); TcpPocket pocket = new TcpPocket(); try { clientSocket.Connect(new IPEndPoint(Ip, Port)); if (clientSocket.Connected) { if (workThread != null) { workThread.Abort(); workThread = null; } workThread = new Thread(ConnectionThread); workThread.Start(clientSocket); if (clientSocket.Connected) {//测试连接 clientSocket.Send(Encoding.UTF8.GetBytes(pocket.ConStructCommand(SocketCommand.ActConnected, SocketCommand.True, SocketCommand.IdfRemoter, "testConnect"))); } return(true); } else { return(false); } } catch (Exception ex) { return(false); } }
public void StopAcceptLoopThread()//停止监听连接 { TcpPocket pocket = new TcpPocket(); SendEverSocketMessage(pocket.ConStructCommand(SocketCommand.ActAbortSocket, SocketCommand.True, SocketCommand.IdfServer, "断开")); //通知remot //清理remote foreach (var tmp in threadBuf.ToList()) { if (tmp.Value != null) { tmp.Value.Abort(); } } RemoteSocketLiveTime.Clear(); RemotClientSockets.Clear(); threadBuf.Clear(); //清理本地 if (serverSocket != null) { //serverSocket在localThread线程中循环阻塞式的等待socket连接,如果serverSocket没有close() //localThread就无法退出,localThread.abort()函数会卡死 serverSocket.Close(); } serverSocket = null; if (localThread != null) { localThread.Abort(); } localThread = null; }
private void ConnectionThread(object clientsocket) //接收数据 { Socket connection = (Socket)clientsocket; byte[] recBufff = new byte[BUFFSIZE]; TcpPocket pocket = new TcpPocket(); while (connection.Connected) { try { int receiveNumber = connection.Receive(recBufff); string recStr = Encoding.UTF8.GetString(recBufff, 0, receiveNumber); if (!pocket.AnalyseCommand(recStr)) { continue; } if (pocket.Command == SocketCommand.ActConnected) { if (pocket.Result == SocketCommand.True) { Event_RecieveMsg(this, new ReceiveArgs(pocket.Command, pocket.Result, pocket.Identify, "服务器连接成功!")); // _time.Start(); } } else if (pocket.Command == SocketCommand.ActAbortSocket)//退出 { if (Event_RecieveMsg != null) { Event_RecieveMsg(this, new ReceiveArgs(pocket.Command, pocket.Result, pocket.Identify, "断开连接")); } break; //跳出当前循环,线程退出 } else if (pocket.Command == SocketCommand.ActMsg) //消息 { if (Event_RecieveMsg != null) { Event_RecieveMsg(this, new ReceiveArgs(pocket.Command, pocket.Result, pocket.Identify, pocket.ConStructShowMsg())); } } } catch (Exception e) { break; } Thread.Sleep(100); } //退出 if (clientSocket != null && clientSocket.Connected) { clientSocket.Shutdown(SocketShutdown.Both); clientSocket.Close(); } clientSocket = null; workThread = null; }
public void SendMsg(string str)//发送数据 { //if (true == RegisterFlag) //{ // sendStr = SocketCommand.MSG + "_" + UserName + "_" + str; // _sendMessage(sendStr); //} TcpPocket pocket = new TcpPocket(); SendMessage(pocket.ConStructCommand(SocketCommand.ActMsg, SocketCommand.True, UserName, str)); }
public void Disconnect()//断开连接 { TcpPocket pocket = new TcpPocket(); SendMessage(pocket.ConStructCommand(SocketCommand.ActAbortSocket, SocketCommand.True, UserName, "断开")); if (clientSocket != null) { clientSocket = null; } if (workThread != null) { workThread.Abort(); workThread = null; } }
public void SendAnotherSocketMessage(IPEndPoint localip, string str)//发送数据 { TcpPocket pocket = new TcpPocket(); foreach (IPEndPoint ip in RemotClientSockets.Keys) { try { if (localip != ip) { if (RemotClientSockets[ip] != null && RemotClientSockets[ip].Connected) { RemotClientSockets[ip].Send(Encoding.UTF8.GetBytes(pocket.ConStructCommand(SocketCommand.ActMsg, SocketCommand.True, SocketCommand.IdfServer, str))); } } } catch (Exception ex) { //RemotClientSockets[ip].Shutdown(SocketShutdown.Both); } } }
private void _time_Tick(object sender, EventArgs e) { TcpPocket pocket = new TcpPocket(); SendMessage(pocket.ConStructCommand(SocketCommand.ActALive, SocketCommand.True, UserName, "心跳")); }
private void ConnectLoopThread(object clientsocket)//接收远端socket数据处理函数 { Socket Connection = (Socket)clientsocket; IPEndPoint ip = (IPEndPoint)Connection.RemoteEndPoint; // bool registerFlag = false; //注册标志 byte[] result = new byte[BUFFSIZE]; TcpPocket pocket = new TcpPocket(); try { while (true) { int receiveNumber = Connection.Receive(result);//接收数据 if (receiveNumber > 0) { string recStr = Encoding.UTF8.GetString(result, 0, receiveNumber); if (!pocket.AnalyseCommand(recStr)) { continue; } if (pocket.Command == SocketCommand.ActConnected)//连接 { if (Event_ReceiveMsg != null) { Event_ReceiveMsg(this, new ReceiveArgs(pocket.Command, SocketCommand.True, pocket.Identify, pocket.ConStructShowMsg())); } Connection.Send(Encoding.UTF8.GetBytes(pocket.ConStructCommand(SocketCommand.ActConnected, SocketCommand.True, SocketCommand.IdfServer, "欢迎"))); } else if (pocket.Command == SocketCommand.ActAbortSocket)//退出 { if (Event_ReceiveMsg != null) { Event_ReceiveMsg(this, new ReceiveArgs(pocket.Command, pocket.Result, pocket.Identify, pocket.ConStructShowMsg())); } break; } else if (pocket.Command == SocketCommand.ActALive)//心跳 { RemoteSocketLiveTime[ip].Restart(); } //else if (recBuf[0] == SocketCommand.Register)//注册 //{ // registerFlag = RemoteClientRegister(ip, recBuf[1]); // Connection.Send(Encoding.UTF8.GetBytes(SocketCommand.Register + "_"+ registerFlag.ToString())); //} //else if (recBuf[0] == SocketCommand.MSG)//消息 //{ // if (true == registerFlag) // { // for (index = 1; index < recBuf.Length; index++) // { // receiveMsg += recBuf[index]; // if ((index + 1) != recBuf.Length) // { // receiveMsg += "_"; // } // } // RemoteClientMsg(receiveMsg); // if (Event_ReceiveMsg != null) // { // Event_ReceiveMsg(this, new ReceiveArgs(receiveMsg)); // } // } //} else if (pocket.Command == SocketCommand.ActMsg)//消息 { SendAnotherSocketMessage(ip, pocket.ConStructCommand()); if (Event_ReceiveMsg != null) {//触发外部的事件 Event_ReceiveMsg(this, new ReceiveArgs(pocket.Command, pocket.Result, pocket.Identify, pocket.ConStructShowMsg())); } } } if (RemoteSocketLiveTime[ip].ElapsedMilliseconds > SocketCommand.ServerHeartbeatClock) {//心跳机制 break; } Thread.Sleep(10); } } catch (Exception ex) { } //退出 threadBuf[ip] = null; if (RemotClientSockets.Count != 0 && RemotClientSockets[ip] != null && RemotClientSockets[ip].Connected) { RemotClientSockets[ip].Shutdown(SocketShutdown.Both); RemotClientSockets[ip].Close(); RemotClientSockets[ip] = null; } if (RemoteSocketLiveTime.Count != 0 && RemoteSocketLiveTime[ip] != null) { RemoteSocketLiveTime[ip] = null; } }