C # multi-network card Server Listen

zhaozj2021-02-08  212

Time: 2004/5/20 Author: Robert Reference: MSDN Email: zsc771120@yahoo.com.cn Keywords: TcpListener IPEndPoint IPAddress IPAddress.Any Purpose: To help monitor multiple network by IP address troubled friend

Make a Server listener in the VC and BCB, only you need to specify port, then listen, you can't find this function, slowly see MSDN, how to specify IP and Port to listen, so many network cards How should the machine be a program? The following programs explain how to do it.

The TCPListener category provides a simple way to listen to the Listen and accept the input connection requirements in the blocked synchronization mode. You can use TCPClient or Socket to connect TCPListener. Use IpendPoint, native IP address, and connection port numbers, or use only the connection number to establish TCPListener. If you want the Basics Supplier to assign those values, specify anyne to the native IP address and 0 to the local connection number. If you want 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 allow the input to enter the queue waiting, until the STOP method is not called, that is, the MaxConnections discharges into the queue. Use Acceptsocket or AcceptTCPClient to remove the connection from the input connection requirement queue. These two methods will block. If you want to avoid blocked, you can use the Pending method to determine if the connection requires to get in the queue.

Call the Stop method to close TCPListener.

This constructive letter allows you to specify the native IP address and connection port number you want to listen to the input connection. Before using this constructive, you must use the desired native IP address and connection port number to establish iPendPoint. Pass this IpendPoint as a localep parameter to the constructive function.

If you don't care about which local address to be assigned, you can use ipaddress.any as an address parameter to establish iPendPoint, and the basic service provider will assign the most appropriate network address. If you have multiple network interfaces, this may help simplify the application. If you don't care which local connection to use, you can specify 0 as a connection number to create the IPENDPOINT. In this case, the service provider will assign the available connection number between 1024 and 5000. If you use this way, you can explore what local area network address and connection port numbers have been assigned by using the localedPoint property.

Call the Start method to start listening to the connected connection attempt.

The IpendPoint category contains hosts and applications to connect to the communication port information required for host services. The connection point connection to the service is formed by incorporating the communication port number of the host's IP address and service, the IpendPoint category.

[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 ());

}

转载请注明原文地址:https://www.9cbs.com/read-3050.html

New Post(0)