c sharp multi network card Server Listen

  • 2020-05-05 11:49:40
  • OfStack

In VC and BCB, you only need to specify the port and then listen to (Listen). The following program explains how to do it The TcpListener   category provides an easy way to listen to   (Listen)   and accept input connection requirements in blocking synchronization mode. You can use   TcpClient   or   Socket   to connect   TcpListener  . Use the   IPEndPoint  , local   IP   address and port number, or simply port number to create   TcpListener  . If you want the underlying service provider to assign you those values, specify   Any   to the local   IP   address and   0   to the local port number. If you choose to do so, you can use   LocalEndpoint   to identify the information assigned.  
Use the   Start   method to begin listening to the incoming connection requirements.   Start   will queue the incoming connection until either the   Stop   method is called or   MaxConnections   is queued. Use   AcceptSocket   or   AcceptTcpClient   to extract the connection from the incoming connection request queue. These two methods will be blocked. If you want to avoid blocking, you can first use the   Pending   method to determine whether the connection request is available in the queue.  
Call the   Stop   method to close   TcpListener  .  
This constructor lets you specify the native   IP   address and connection port number to hear the input connection attempt. Before using this constructor, you must create   IPEndPoint   with the required native   IP   address and connection port number. Pass this   IPEndPoint   parameter to the constructor as   localEP  .  
If you do not care which local address you want to assign,   IPAddress.Any   can be used as the address parameter to establish   IPEndPoint  , and the basic service provider will assign the most appropriate network address. If you have multiple network interfaces, this can help simplify the application. If you don't care which native port you want to use, you can set up   IPEndPoint   by specifying   0   as the port number. In this case, the service provider will assign an available port number between   1024   and   5000  . If you use this approach, you can explore what LAN addresses and connection port Numbers have been assigned by using the   LocalEndpoint   property.  
Call the   Start   method to begin listening to the incoming connection attempt.  
The IPEndPoint   category contains information about the host and the communication port that the application needs to connect to the host service. By combining the host's   IP   address with the service's communication port number, the   IPEndPoint   class forms the connection point   (Connection   Point) to the service.  
[C#]  
//Creates   an   instance   of   the   TcpListener   class   by   providing   a   local   endpoint.  
IPAddress   ipAddress   =   Dns.Resolve(Dns.GetHostName()).AddressList[0];  
IPEndPoint   ipLocalEndPoint   =   new   IPEndPoint(ipAddress,   11000);  
try{  
TcpListener   tcpListener   =   new   TcpListener(ipLocalEndPoint);  
}  
catch   (   Exception   e   ){  
Console.WriteLine(   e.ToString());  
}  

Related articles: