Under Peer-to-peer network communication based on TCP/IP protocol clusters, each communication application runs its own process, and the data bureau is encapsulated into a datagram for the process of application layer, and the network communication is carried out through TCP or UDP in the transport layer. TCP and UPD, however, use a 16bit port to identify different applications.
For some of the most common web services, they have a well known port to match. For example, the FTP service is using a TCP port for the 21;telnet service with a TCP port of 23 and so on. And for the client is usually not concerned about the port used, only to ensure that the port is unique on the machine, so that the port becomes a temporary port, the temporary port generally between 1024 to 5000.
Generally speaking, a port can only be used by one application at a time. For WCF, when we host a service through a managed application, a port is exclusively used by the application. How to use the same port for multiple boarders
In the following example, I hosted two services, Service1 and Service2 through two different console applications, and two service endpoint addresses shared the same port: 9999.
using(ServiceHost serviceHost = new ServiceHost(typeof(Service1)))
{
serviceHost.AddServiceEndpoint(typeof(IService1), new NetTcpBinding(), "net.tcp://127.0.0.1:9999/service1");
serviceHost.Open();
Console.Read();
}
using(ServiceHost serviceHost = new ServiceHost(typeof(Service2)))
{
serviceHost.AddServiceEndpoint(typeof(IService2), new NetTcpBinding(), "net.tcp://127.0.0.1:9999/service2");
serviceHost.Open();
Console.Read();
}
When we run these two service boarding applications, the first one can run normally, but for the second one, it throws the following Adressalreadyinuseexception exception, the error message is:
There is already a listener on the IP endpoint 127.0.0.1:9999. Make sure that the endpoint is not tried multiple times in your application and that no other application is listening on the endpoint.
In this section, we'll show you how to solve a problem where this port is used exclusively by one application, allowing different listeners to share the same port. Before that, we need to know what the real meaning of port sharing is.