UDP socket with multiple ports

R

Robin Theilade

Hello,

I'm having some trouble creating an UDP socket that can listen on multiple
ports.

This is my code so far, it might give you a better idea of what I am trying
to accomplish. The problem using this code is when the Bind is called I get
a SocketException because the socket is already bound.

this.udpSocket = new Socket( AddressFamily.InterNetwork, SocketType.Dgram,
ProtocolType.Udp );

this.udpSocket.SetSocketOption( SocketOptionLevel.Socket,
SocketOptionName.ReuseAddress, 1 );

this.udpSocket.Bind( new IPEndPoint( IPAddress.Any, 7000 ) );

this.udpSocket.Bind( new IPEndPoint( IPAddress.Any, 7001 ) );



Any suggestions how to implement such a socket?



Best regards,

Robin Theilade
 
K

Kevin Spencer

A single socket can only be bound to one host/port pair.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Complex things are made up of
lots of simple things.
 
R

Robin Theilade

Yes that's whats troubling me. So the solution is either to set the Socket
to listen to every port (IP raw mode) or just one? There aren't any class
that handles the listening from multiple ports, doesn't have to be Socket
class.

Best regards,
Robin Theilade
 
V

Vadym Stetsyak

Hello, Robin!

Yes that's whats troubling me. So the solution is either to set the Socket
to listen to every port (IP raw mode) or just one? There aren't any class
that handles the listening from multiple ports, doesn't have to be Socket
class.

AFAIK there is no such class. You can develop your own ( it will aggregate multiple sockets, socket per port... ).
Usage of raw sockets is constained under XP SP2.

Why do you need such functionality? What task you want to accomplish with multiport solution?
--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
 
R

Robin Theilade

Hi Vadym,

If Ethereal can sniff traffic in raw mode it should be possible somehow on a
XP SP2, maybe I should try the WinPCap library Ethereal uses.

Anyway, the reason why I need to receive data from more than one UDP port is
that I want to build a mod for SA-MP (Grand Theft Auto - San Andreas -
Multiplayer) and it used both 7777 and 7778 and maybe some more. The mod
must be implemented as a proxy between the client and the server. Maybe I
should look into the DirectX library, it's very normal for network games to
use UDP and often multiple when it's a Microsoft game.

Best regards,
Robin Theilade
 
R

Robin Theilade

Hi Vadym,

I'll take a look at DirectPlay and if it doesn't have build in support for
multiple ports in one host object, I'll implement my own.

Thanks for the help.

Best regards,
Robin Theilade
 
K

Kevin Spencer

Keep in mind that a Socket, by definition, represents a single unique
address, which is the combination of an IP address (which has 65536 ports)
and a port number. So, if you use Sockets, MS or otherwise, you're limited
to one port per Socket.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

The man who questions opinions is wise.
The man who quarrels with facts is a fool.

Robin Theilade said:
Hi Vadym,

I'll take a look at DirectPlay and if it doesn't have build in support for
multiple ports in one host object, I'll implement my own.

Thanks for the help.

Best regards,
Robin Theilade
 
R

Robin Theilade

Hi Kevin,

You're right about that, but still it is not unusual that someone builds
some kind of proxy and my guess was that the .NET framework already such a
class to handle multiple ports. Consider the
opposite case where you have serveral clients; here the Socket class
supports multicast to send the same data to all the clients. I know that it
is not interely the same but it proves that the Socket class in special
cases can handle multiple addresses. So it was just a small hope.

Best regards,
Robin Theilade

Kevin Spencer said:
Keep in mind that a Socket, by definition, represents a single unique
address, which is the combination of an IP address (which has 65536 ports)
and a port number. So, if you use Sockets, MS or otherwise, you're limited
to one port per Socket.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

The man who questions opinions is wise.
The man who quarrels with facts is a fool.
 
V

Vadym Stetsyak

Hello, Robin!

RT> You're right about that, but still it is not unusual that someone
RT> builds some kind of proxy and my guess was that the .NET framework
RT> already such a class to handle multiple ports. Consider the
RT> opposite case where you have serveral clients; here the Socket class
RT> supports multicast to send the same data to all the clients. I know
RT> that it is not interely the same but it proves that the Socket class in
RT> special cases can handle multiple addresses. So it was just a small
RT> hope.

Actually multicasting is also happening on single address, multicast address ::cool:. So, there is still
host/port couple out there.

--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
 
W

William Stacey [MVP]

Why not use two sockets??

--
William Stacey [MVP]

message | Hello,
|
| I'm having some trouble creating an UDP socket that can listen on multiple
| ports.
|
| This is my code so far, it might give you a better idea of what I am
trying
| to accomplish. The problem using this code is when the Bind is called I
get
| a SocketException because the socket is already bound.
|
| this.udpSocket = new Socket( AddressFamily.InterNetwork, SocketType.Dgram,
| ProtocolType.Udp );
|
| this.udpSocket.SetSocketOption( SocketOptionLevel.Socket,
| SocketOptionName.ReuseAddress, 1 );
|
| this.udpSocket.Bind( new IPEndPoint( IPAddress.Any, 7000 ) );
|
| this.udpSocket.Bind( new IPEndPoint( IPAddress.Any, 7001 ) );
|
|
|
| Any suggestions how to implement such a socket?
|
|
|
| Best regards,
|
| Robin Theilade
|
|
 
R

Robin Theilade

Hi William,

It is the solution that I have now, except that I have four sockets two for
each way through my proxy. But it seems a bit like I would be abusing my
computers resources if I had to bould a proxy that took every port from 1 to
1024. It would mean that I had to use 2048 sockets. But thats the only way,
theres nothing to do about it.

Best regards,
Robin Theilade
 
K

Kevin Spencer

Here's a great MSDN article about setting up a reliable UDP broadcast (using
only one port for many clients):

http://msdn.microsoft.com/msdnmag/issues/06/02/UDP/

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

The man who questions opinions is wise.
The man who quarrels with facts is a fool.

Robin Theilade said:
Hi Vadym,

You're probably right about that.

I was going to lookup the DirectPlay you mentioned when I found this FAQ
entry http://www.thezbuffer.com/articles/387.aspx. I guess I'll have to go
with the multiple sockets solution.

Thanks for the help.

Best regards,
Robin Theilade
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top