using  System ; 
using  System. Collections. Generic ; 
using  System. Text ; 
#region  命名空间 
using  System. Net ; 
using  System. Net. Sockets ; 
using  System. Threading ; 
using  UnityEngine ; 
#endregion  namespace  AsynServerConsole 
{ public  class  AsynTcpServer :  MonoBehaviour { private  static  Dictionary< string ,  Socket>   socketList =  new  Dictionary< string ,  Socket>  ( ) ; private  void Start ( ) { StartListening ( ) ; } float ; private  void Update ( ) { ttt +=  Time. deltaTime; if  ( ttt> 5f ) { ttt =  0f ; if  ( socketList. Count >  0 ) { foreach  ( var in  socketList) { } } else  { } } } Socket  TcpServer; #region  Tcp协议异步监听 public  void StartListening ( ) { IPEndPoint  serverIp =  new  IPEndPoint ( IPAddress. Parse ( "192.168.15.72" ) ,  8686 ) ; TcpServer =  new  Socket ( AddressFamily. InterNetwork,  SocketType. Stream,  ProtocolType. Tcp) ; TcpServer. Bind ( serverIp) ; TcpServer. Listen ( 100 ) ; Debug. Log ( "异步开启监听..." ) ; AsynAccept ( TcpServer) ; } #endregion  #region  异步连接客户端 public  void AsynAccept ( Socket  tcpServer) { tcpServer. BeginAccept ( asyncResult => { Socket  tcpClient =  tcpServer. EndAccept ( asyncResult) ; string =  tcpClient. RemoteEndPoint. ToString ( ) ; socketList. Add ( str,  tcpClient) ; Debug. Log ( "收到连接============="  +  tcpClient. RemoteEndPoint. ToString ( ) ) ; AsynSend ( tcpClient,  "收到客户端连接..." ) ; AsynAccept ( tcpServer) ; AsynRecive ( tcpClient) ; } ,  null ) ; } #endregion  #region  异步接受客户端消息 public  void AsynRecive ( Socket  tcpClient) { byte [ ] =  new  byte [ 1024 ] ; try { tcpClient. BeginReceive ( data,  0 ,  data. Length,  SocketFlags. None, asyncResult => { int =  tcpClient. EndReceive ( asyncResult) ; if  ( length== 0 ) { Debug. Log ( "客户端退出====" + tcpClient. RemoteEndPoint. ToString ( ) ) ; foreach  ( var in  socketList) { if  ( item. Key. Contains ( tcpClient. RemoteEndPoint. ToString ( ) ) ) { socketList. Remove ( item. Key) ; } } return ; } Debug. Log ( tcpClient. RemoteEndPoint. ToString ( ) + "收到客户端消息:{length}====="  +  length +  "___"  +  Encoding. UTF8. GetString ( data) ) ;  AsynSend ( tcpClient,  "收到客户端消息..." ) ; AsynRecive ( tcpClient) ; } ,  null ) ; } catch  ( Exception  ex) { Debug. Log ( "异常信息:"  +  ex. Message) ; } } #endregion  #region  异步发送消息 public  void AsynSend ( Socket  tcpClient,  string ) { byte [ ] =  Encoding. UTF8. GetBytes ( message) ; try { tcpClient. BeginSend ( data,  0 ,  data. Length,  SocketFlags. None,  asyncResult => { int =  tcpClient. EndSend ( asyncResult) ; Debug. Log ( "服务端完成发送消息====="  +  length+ "___"  + message) ; } ,  null ) ; } catch  ( Exception  ex) { Debug. Log ( "发送消息异常信息====="  +  ex. Message) ; } } #endregion  private  void HandleClient ( Socket  clientSocket) { byte [ ] =  new  byte [ 1024 ] ; try { int =  clientSocket. Receive ( buffer) ; if  ( bytesRead >  0 ) { Debug. Log ( "Received: {0}"  +  Encoding. ASCII. GetString ( buffer,  0 ,  bytesRead) ) ; } else { Debug. Log ( "Client closed the connection." ) ; } } catch  ( SocketException  se) { Debug. Log ( $"Socket error:  { se. Message } " ) ; } finally { clientSocket. Close ( ) ; } } private  void OnDestroy ( ) { Debug. Log ( socketList. Count) ; foreach  ( KeyValuePair< string ,  Socket>   item in  socketList) { item. Value. Dispose ( ) ; item. Value. Close ( ) ; } if  ( TcpServer. Connected) { TcpServer. Shutdown ( SocketShutdown. Both) ; } TcpServer. Dispose ( ) ; TcpServer. Close ( ) ; } [ ContextMenu ( "客户端数量" ) ] public  void DebugClientCount ( )  { Debug. Log ( "连接数量==="  +  socketList. Count) ; } } 
}