C # multi-nic Server Listen

Source: Internet
Author: User
Tags constructor connect port number
Server VC and BCB to do a server listener, only need to specify the port, and then listen (Listen) on the line. In C # can not find this function, and slowly look at MSDN, how to specify the IP and port to monitor, so many network card machine should be how to process it? The following procedure can explain how to do it.



The TcpListener category provides an easy way to listen to (Listen) and accept incoming connection requirements in a blocked sync mode. You can use TcpClient or sockets to connect TcpListener. Use the IPEndPoint, native IP address and connection port number, or simply establish the TcpListener with the connection number. If you want the base service provider to assign those values to you, specify any to give the local port number to the native IP address and 0. If you choose to do this, you can use Localendpoint to identify the assigned information.

Use the Start method to start listening for incoming connection requirements. Start will let the incoming connection enter the queue until either the Stop method is called or the maxconnections is queued. Use AcceptSocket or accepttcpclient to request the queue to remove the connection from the incoming connection. These two methods will be blocked. If you want to avoid blocking, you can use the Pending method to determine whether a connection request can be made in a queue.

Call Stop method to close TcpListener.



This constructor allows you to specify the native IP address and connection port number to listen to the input connection attempt. Before using this constructor, you must use the required native IP address and connection port number to establish the IPEndPoint. Pass this ipendpoint as a localep argument to a constructor.

If you don't care which native address you want to assign, you can use Ipaddress.any as the address parameter to establish the IPEndPoint, and the base service provider will assign the most appropriate network address. If you have more than one network interface, this may help simplify your application. If you don't care which native port you want to use, you can specify 0 as the port number to establish the IPEndPoint. In this case, the service provider will assign the available port number between 1024 and 5000. If you use this approach, you can explore what local area network address and connection port number has been assigned by using the Localendpoint property.

Call the Start method to begin listening for the input connection attempt.



The IPEndPoint category contains the communication port information required by the host and the application to connect 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 (Connection points) connected to the service.



[C #]

Creates an instance to 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 Article

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.