"Socke" C # Socket Port multiplexing-Multiple host header bindings

Source: Internet
Author: User

What is Port multiplexing: because in the implementation of the Winsock, the binding to the server can be multi-bound, and in determining who to use the multi-binding, according to a principle who specifies the most clear who will pass the package to whom, and no permissions. This multi-binding is called Port multiplexing. Second, how we implement socket Port multiplexing: In fact, we want to implement port multiplexing is very simple, we just use the SetSocketOption function to set the socket option. MSDN explains this: The socket option determines the behavior of the current socket. For options with a Boolean data type, specify a value other than 0 to enable the option, and specify a value of 0 to disable the option. For options with an integer data type, specify the appropriate value. The Socket options are grouped according to the level of protocol support. Let's take a look at how this function is used:

 Public void SetSocketOption (       socketoptionlevel optionlevel,      socketoptionname optionname,     int

Parameter Optionlevel One of the socketoptionlevel values. One of the Optionname socketoptionname values. OptionValue The value of this option. All of the above parameters can be seen on MSDN. I don't have much to say here. Here we
Optionlevel parameter transmission
Socketoptionlevel.socket
Optionname parameter socketoptionname.reuseaddress;optionvalue a non-0 value, I pass true, if you want to disable it, pass false. Such as:

True

Specifically, let's look at the following code:

//We first create a socket:
Socket Socket1; IPEndPoint localEP1=NewIPEndPoint (Ipaddress.any,20000); Socket1=Newsockets (AddressFamily.InterNetwork, SocketType.Stream, protocoltype.tcp); Socket1. Bind (localEP1);//Build a second socket:socket Socket2; IPEndPoint localEP2=NewIPEndPoint (Ipaddress.parse ("127.0.0.1"),20000); Socket2=Newsockets (AddressFamily.InterNetwork, SocketType.Stream, protocoltype.tcp); Socket2. SetSocketOption (Socketoptionlevel.socket, socketoptionname.reuseaddress,true); //Please note this sentence. The reuseaddress option is set to true to allow the socket to be bound to an address that is already in use. Socket2. Bind (localEP2)

Please note this sentence. The reuseaddress option is set to true to allow the socket to be bound to an address that is already in use. Socket2. Bind (LOCALEP); This way Socket1 and Socket2 are bound to the same port. According to one principle, who specifies the most specific to whom to pass the package, meaning to determine who the connection is given by the IP address of the multiple sockets bind. such as: Socketa bind Ipaddress.any,socketb bind 192.168.0.5, Then the client connection 192.168.0.5, the connection will be handed over to Socketb to handle, and the connection of the other IP address will be given to Socketa to handle. The real use of port multiplexing is primarily in server programming: When a server needs to be restarted, it often encounters a situation where the port has not been completely shut down, and if the port is not set, the binding cannot be completed because the port is also in a state that is bound by another set of interfaces.

"Socke" C # Socket Port multiplexing-Multiple host header bindings

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.