C # multi-nic server listen

Source: Internet
Author: User

Listen to a server in VC and BCB Program You only need to specify the port and then listen (Listen. I can't find this function in C #. I'll take a look at msdn. How do I need to specify IP addresses and ports for listening? How do I write programs for machines with multiple NICs? The following program can explain how to do this.
The tcplistener category provides a simple method for listening (Listen) and receiving input connection requirements in blocked synchronous modes. You can use tcpclient or socket to connect to tcplistener. Use ipendpoint, local IP address, and connection port number, or use the connection port number to create a tcplistener. If you want the base service supplier to assign those values to you, specify any to the local IP address and 0 to the local port number. If you choose to do this, you can use localendpoint to identify the assigned information.
Use the start method to start listening to the input connection requirements. Start will let the input connection wait in the queue until the stop method is called or maxconnections has been queued. Use acceptsocket or accepttcpclient to retrieve the connection from the input connection request queue. These two methods will be blocked. To avoid blocking, you can use the pending method to determine whether the connection requirements can be obtained in the queue.
Call the stop method to disable tcplistener.
This constructor allows you to specify the IP address and port number of the local machine that you want to listen. Before using this constructor, you must use the required local IP address and connection port number to create an ipendpoint. This ipendpoint is passed to the constructor as the localep parameter.
If you do not care which local address you want to assign, you can use IPaddress. Any as the address parameter to create an ipendpoint, and the basic service supplier will assign the most appropriate network address. If you have multiple network interfaces, this may help simplify the application. If you do not care which local connection port you want to use, you can specify 0 as the connection port number to create an ipendpoint. In this case, the service supplier assigns an available port number between 1024 and 5000. If you use this method, you can use the localendpoint attribute to find out which local network address and connection port number have been assigned.
Call the start method to start listening to input connection attempts.
The ipendpoint category contains the communication port information required to connect the host and application to the host service. By combining the IP address of the host and the communication port number of the service, the ipendpoint category forms the connection point to the service ).
[C #]
// Creates an instance of the tcplistener class by providing a local endpoint.
IPaddress = DNS. Resolve (DNS. gethostname (). Addresslist [0];
Ipendpoint iplocalendpoint = new ipendpoint (IPaddress, 11000 );
Try {
Tcplistener = new tcplistener (iplocalendpoint );
}
Catch (exception e ){
Console. writeline (E. tostring ());
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.