c#_监听TCP连接请求

1、use TcpListener
TcpListener lsn = new TcpListener( int port );
lsn.Start();
Socket socket = lsn.AcceptSocket();

2、use Socket(need to Bind)
Socket lsnSocket = new Socket( AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp );
IPEndPoint ipep = new IPEndPoint( IPAddress.Any, port );
lsnSocket.Bind( ipep );   //Bind the lsnSocket
lsnSocket.Listen( 5 );    //5 is the queue's length
Socket acSocket = lsnSocket.Accept();

你可能感兴趣的:(程序设计)