Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bind multiple listener to the same port

Tags:

c#

udpclient

I am using UdpClient class in .net 3.5 I need to bind multiple applications to the same port .

So, if UDP servers broadcast any request - all the applications thats listen on the port can receive the message but the problem is, when I try bind to an application to the same port, only one application receive the message and the other does not.

Below is some sample code for the two application:

    UdpClient udpClient = new UdpClient();
    Thread thread;
    IPEndPoint endPoint = new IPEndPoint(IPAddress.Loopback, 11000);
    public Form1()
    {
        //CheckForIllegalCrossThreadCalls = false;

        InitializeComponent();
        udpClient.ExclusiveAddressUse = false;
        udpClient.Client.SetSocketOption(
        SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
        udpClient.Client.Bind(endPoint);
    }

    private void MainForm_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Escape)
        {
            thread.Abort();
            udpClient.Close();
            Close();
        }
    }

    private void ReceiveMessage()
    {
        //while (true)
        //{
        // IPEndPoint remoteIPEndPoint = new IPEndPoint(IPAddress.Any, 11000);
        //  byte[] content = udpClient.Receive(ref endPoint);
        udpClient.BeginReceive(new AsyncCallback(Read_Callback), null);

        //if (content.Length > 0)
        //{
        //    string message = Encoding.ASCII.GetString(content);

        //    this.Invoke(myDelegate, new object[] { message });
        //}
        // }
    }

    public void Read_Callback(IAsyncResult ar)
    {
        try
        {
            byte[] buffer = udpClient.EndReceive(ar, ref endPoint);
            // Process buffer
            string s = Encoding.ASCII.GetString(buffer);
            // richTextBox1.Text = s;
            udpClient.BeginReceive(new AsyncCallback(Read_Callback), null);

        }
        catch (Exception ex)
        { }
    }

PS : I am unable to figure out the reason or am I missing something. ?

like image 853
shujaat siddiqui Avatar asked Apr 02 '14 11:04

shujaat siddiqui


People also ask

Can we use same port for 2 listeners?

For TCP, no. You can only have one application listening on the same port at one time. Now if you had 2 network cards, you could have one application listen on the first IP and the second one on the second IP using the same port number. For UDP (Multicasts), multiple applications can subscribe to the same port.

Can multiple sockets bind to same port?

When multiple sockets are allowed to bind to the same port, other services on that port may be stolen or spoofed. On most systems, a combination of setting the SO_REUSEADDR socket option, and a call to bind() allows any process to bind to a port to which a previous process has bound with INADDR_ANY.

Can two UDP sockets have the same port number?

Yes, it is also possible to have multiple sockets using a single UDP port. With the caveat that only broadcast & multicast packets are going to be multiplexed, unicast packets will only be delivered to the first socket.


1 Answers

That's the nature of sockets. Even in cases (such as UDP) where multiple applications can access the same port, the data is handed out first-come, first-serve. UDP is also designed with minimum overhead, so there isn't even an opportunity to "check the queue," like you (hypothetically) could with TCP.

It's designed around having multiple processes share a server load, alternating who receives the request based on who's idle.

You'd need to build something external to get around this, like a retransmission protocol or a database to make sure every inbound message is shared.

If you can deal with the changes, a smarter way to handle this would be UDP Multicast, where multiple programs essentially enroll to receive group messages. In that case, the single-port restriction can (and should) be abandoned.

like image 64
John C Avatar answered Sep 24 '22 08:09

John C