Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Why can't I have multiple listeners on a UDP port?

390 views
Skip to first unread message

David Thielen

unread,
May 9, 2008, 12:28:51 PM5/9/08
to
Hi;

In my application I call:
new UdpClient(1707);

to listen on that UDP port. The problem is if I run 2 instances of my
app, the second one throws an exception on this call. Why? It seems to
me that as it's a UDP broadcast, I should be able to have multiple
listeners on that port.

The exception is:
System.Net.Sockets.SocketException occurred
Message="Only one usage of each socket address (protocol/network
address/port) is normally permitted"
Source="System"
ErrorCode=10048
NativeErrorCode=10048
StackTrace:
at System.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot,
SocketAddress socketAddress)
at System.Net.Sockets.Socket.Bind(EndPoint localEP)
at System.Net.Sockets.UdpClient..ctor(Int32 port, AddressFamily
family)
at System.Net.Sockets.UdpClient..ctor(Int32 port)
at net.windward.env.LicenseDaemon.CheckThread.ThreadProc() in
C:\src\kahuna\DotNetEngine\WindwardReports\net\windward\env\LicenseDaemon.jsl:line
70


??? - thanks - dave


david@at-at-at@windward.dot.dot.net
Windward Reports -- http://www.WindwardReports.com
me -- http://dave.thielen.com

Cubicle Wars - http://www.windwardreports.com/film.htm

Charles Wang [MSFT]

unread,
May 12, 2008, 1:22:55 AM5/12/08
to
Hi Dave,
I understand that you would like to have your miltiple application
instances to bind the same UDP port number, however you encountered the
error "only one usage of each socket address (protocol/networkaddress/port)
is normally permitted" when more than one application instance calls "new
UdpClient(1707)".
If I have misunderstood, please let me know.

The error is expected since by default you can have only one instance bind
the same port. To bind multiple sockets to the same port number, you need
to specify the socket option ReuseAddress and also do not use the
constructor here. I recommend that you refer to my code as following:
===================================
try
{
IPEndPoint localpt = new
IPEndPoint(IPAddress.Parse("127.0.0.1"),Int32.Parse(txtPort.Text));

UdpClient udpServer = new UdpClient();
udpServer.Client.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.ReuseAddress, true);
udpServer.Client.Bind(localpt);
MessageBox.Show("OK");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
======================================

You may also refer to this article,
http://msdn.microsoft.com/en-us/library/ms740668.aspx, from which you can
find the following description:
============================
WSAEADDRINUSE
10048
Address already in use.
Typically, only one usage of each socket address (protocol/IP address/port)
is permitted. This error occurs if an application attempts to bind a socket
to an IP address/port that has already been used for an existing socket, or
a socket that was not closed properly, or one that is still in the process
of closing. For server applications that need to bind multiple sockets to
the same port number, consider using setsockopt (SO_REUSEADDR). Client
applications usually need not call bind at all - connect chooses an unused
port automatically. When bind is called with a wildcard address (involving
ADDR_ANY), a WSAEADDRINUSE error could be delayed until the specific
address is committed. This could happen with a call to another function
later, including connect, listen, WSAConnect, or WSAJoinLeaf.
=====================================

Hope this helps.

If you have any other questions or concerns, please feel free to let me
know. Have a nice day!

Best regards,
Charles Wang
Microsoft Online Community Support
===========================================================
Delighting our customers is our #1 priority. We welcome your
comments and suggestions about how we can improve the
support we provide to you. Please feel free to let my manager
know what you think of the level of service provided. You can
send feedback directly to my manager at: msd...@microsoft.com.
===========================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for
non-urgent issues where an initial response from the community
or a Microsoft Support Engineer within 1 business day is acceptable.
Please note that each follow up response may take approximately
2 business days as the support professional working with you may
need further investigation to reach the most efficient resolution.
The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by
contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
============================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
=========================================================

David Thielen

unread,
May 13, 2008, 5:52:21 PM5/13/08
to
perfect - thanks

david@at-at-at@windward.dot.dot.net

Ben Voigt [C++ MVP]

unread,
May 21, 2008, 6:04:25 PM5/21/08
to
David Thielen wrote:
> perfect - thanks

But does it result in all listeners receiving the broadcast, or just one?
And if just one, is it the first, last, or random?

David Thielen

unread,
May 21, 2008, 10:39:38 PM5/21/08
to
In our tests it appears to be all of them.

On Wed, 21 May 2008 17:04:25 -0500, "Ben Voigt [C++ MVP]"
<r...@nospam.nospam> wrote:

>David Thielen wrote:
>> perfect - thanks
>
>But does it result in all listeners receiving the broadcast, or just one?
>And if just one, is it the first, last, or random?
>

Uri Kluk

unread,
Oct 8, 2008, 1:43:01 AM10/8/08
to
I have a similar issue but I am not using multicast:

In my case the SERVER sends in the UDP payload a token and the endpoint
CLIENT returns the matching token... the protocol is always request/response.

If the server tries to invoke this req/resp to more than one client
concurrently it throws the SocketException.

If UDP is stateless why can't I create 2+ listeners paying attention to
different IP addresses but on the same port?

Do I need different ports?

0 new messages