WCF follow-on tour: TCP port sharing

Source: Internet
Author: User
Tags web services

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.

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.