Home All Groups Group Topic Archive Search About

UDP socket with multiple ports

Author
18 May 2006 8:41 AM
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

Author
18 May 2006 9:39 AM
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.

Show quote
"Robin Theilade" <nospam.robin.theilade.nospam@nospam.gmail.com> wrote in
message news:DqWag.37$vJ7.14@news.get2net.dk...
> 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
>
>
Author
18 May 2006 9:51 AM
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

Show quote
"Kevin Spencer" <kevin@DIESPAMMERSDIEtakempis.com> wrote in message
news:uygfo7leGHA.3456@TK2MSFTNGP05.phx.gbl...
>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.
>
> "Robin Theilade" <nospam.robin.theilade.nospam@nospam.gmail.com> wrote in
> message news:DqWag.37$vJ7.14@news.get2net.dk...
>> 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
>>
>>
>
>
Author
18 May 2006 11:30 AM
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
Author
18 May 2006 11:53 AM
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

Show quote
"Vadym Stetsyak" <vady***@ukr.net> wrote in message
news:%23zjQi5meGHA.5040@TK2MSFTNGP03.phx.gbl...
> 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
Author
18 May 2006 12:22 PM
Vadym Stetsyak
Hello, Robin!

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

Network "sniffing" approach is not what you want.

The option for you is to develop a custom class that will aggregate several sockets to simulate multiport messaging.

DirectX has DirectPlay technology that is used for network communication.
( http://en.wikipedia.org/wiki/DirectPlay )
( http://www.informit.com/articles/article.asp?p=26236 )



--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
Author
18 May 2006 12:49 PM
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

Show quote
"Vadym Stetsyak" <vady***@ukr.net> wrote in message
news:eOd%23yWneGHA.536@TK2MSFTNGP02.phx.gbl...
> Hello, Robin!
>
>  RT> Anyway, the reason why I need to receive data from more than one UDP
> RT> port is that I want to build a mod for SA-MP (Grand Theft Auto - San
> RT> Andreas - Multiplayer) and it used both 7777 and 7778 and maybe some
> RT> more. The mod must be implemented as a proxy between the client and
> the
> RT> server. Maybe I should look into the DirectX library, it's very normal
> RT> for network games to use UDP and often multiple when it's a Microsoft
> RT> game.
>
> Network "sniffing" approach is not what you want.
>
> The option for you is to develop a custom class that will aggregate
> several sockets to simulate multiport messaging.
>
> DirectX has DirectPlay technology that is used for network communication.
> ( http://en.wikipedia.org/wiki/DirectPlay )
> ( http://www.informit.com/articles/article.asp?p=26236 )
>
>
>
> --
> Regards, Vadym Stetsyak
> www: http://vadmyst.blogspot.com
Author
18 May 2006 2:05 PM
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.

Show quote
"Robin Theilade" <nospam.robin.theilade.nospam@nospam.gmail.com> wrote in
message news:F3_ag.86$0I3.21@news.get2net.dk...
> 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
>
> "Vadym Stetsyak" <vady***@ukr.net> wrote in message
> news:eOd%23yWneGHA.536@TK2MSFTNGP02.phx.gbl...
>> Hello, Robin!
>>
>>  RT> Anyway, the reason why I need to receive data from more than one UDP
>> RT> port is that I want to build a mod for SA-MP (Grand Theft Auto - San
>> RT> Andreas - Multiplayer) and it used both 7777 and 7778 and maybe some
>> RT> more. The mod must be implemented as a proxy between the client and
>> the
>> RT> server. Maybe I should look into the DirectX library, it's very
>> normal
>> RT> for network games to use UDP and often multiple when it's a Microsoft
>> RT> game.
>>
>> Network "sniffing" approach is not what you want.
>>
>> The option for you is to develop a custom class that will aggregate
>> several sockets to simulate multiport messaging.
>>
>> DirectX has DirectPlay technology that is used for network communication.
>> ( http://en.wikipedia.org/wiki/DirectPlay )
>> ( http://www.informit.com/articles/article.asp?p=26236 )
>>
>>
>>
>> --
>> Regards, Vadym Stetsyak
>> www: http://vadmyst.blogspot.com
>
>
Author
18 May 2006 3:04 PM
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

Show quote
"Kevin Spencer" <kevin@DIESPAMMERSDIEtakempis.com> wrote in message
news:emgskQoeGHA.4276@TK2MSFTNGP03.phx.gbl...
> 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" <nospam.robin.theilade.nospam@nospam.gmail.com> wrote in
> message news:F3_ag.86$0I3.21@news.get2net.dk...
>> 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
>>
>> "Vadym Stetsyak" <vady***@ukr.net> wrote in message
>> news:eOd%23yWneGHA.536@TK2MSFTNGP02.phx.gbl...
>>> Hello, Robin!
>>>
>>>  RT> Anyway, the reason why I need to receive data from more than one
>>> UDP
>>> RT> port is that I want to build a mod for SA-MP (Grand Theft Auto - San
>>> RT> Andreas - Multiplayer) and it used both 7777 and 7778 and maybe some
>>> RT> more. The mod must be implemented as a proxy between the client and
>>> the
>>> RT> server. Maybe I should look into the DirectX library, it's very
>>> normal
>>> RT> for network games to use UDP and often multiple when it's a
>>> Microsoft
>>> RT> game.
>>>
>>> Network "sniffing" approach is not what you want.
>>>
>>> The option for you is to develop a custom class that will aggregate
>>> several sockets to simulate multiport messaging.
>>>
>>> DirectX has DirectPlay technology that is used for network
>>> communication.
>>> ( http://en.wikipedia.org/wiki/DirectPlay )
>>> ( http://www.informit.com/articles/article.asp?p=26236 )
>>>
>>>
>>>
>>> --
>>> Regards, Vadym Stetsyak
>>> www: http://vadmyst.blogspot.com
>>
>>
>
>
Author
18 May 2006 3:37 PM
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 :8-). So, there is still
host/port couple out there.

--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
Author
19 May 2006 7:51 AM
Robin Theilade
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

Show quote
"Vadym Stetsyak" <vady***@ukr.net> wrote in message
news:eFdvtDpeGHA.3484@TK2MSFTNGP02.phx.gbl...
> 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 :8-). So, there is still
> host/port couple out there.
>
> --
> Regards, Vadym Stetsyak
> www: http://vadmyst.blogspot.com
Author
19 May 2006 11:20 AM
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.

Show quote
"Robin Theilade" <nospam.robin.theilade.nospam@nospam.gmail.com> wrote in
message news:SNebg.12$%J5.1@news.get2net.dk...
> 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
>
> "Vadym Stetsyak" <vady***@ukr.net> wrote in message
> news:eFdvtDpeGHA.3484@TK2MSFTNGP02.phx.gbl...
>> 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 :8-). So, there is still
>> host/port couple out there.
>>
>> --
>> Regards, Vadym Stetsyak
>> www: http://vadmyst.blogspot.com
>
>
Author
19 May 2006 1:59 PM
Robin Theilade
Hi Kevin,

That article looks interesting, thanks!

Best regards,
Robin Theilade

Show quote
"Kevin Spencer" <kevin@DIESPAMMERSDIEtakempis.com> wrote in message
news:O4dJQZzeGHA.2456@TK2MSFTNGP04.phx.gbl...
> 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" <nospam.robin.theilade.nospam@nospam.gmail.com> wrote in
> message news:SNebg.12$%J5.1@news.get2net.dk...
>> 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
>>
>> "Vadym Stetsyak" <vady***@ukr.net> wrote in message
>> news:eFdvtDpeGHA.3484@TK2MSFTNGP02.phx.gbl...
>>> 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 :8-). So, there is still
>>> host/port couple out there.
>>>
>>> --
>>> Regards, Vadym Stetsyak
>>> www: http://vadmyst.blogspot.com
>>
>>
>
>
Author
19 May 2006 3:12 AM
William Stacey [MVP]
Why not use two sockets??

--
William Stacey [MVP]

Show quote
"Robin Theilade" <nospam.robin.theilade.nospam@nospam.gmail.com> wrote in
message news:DqWag.37$vJ7.14@news.get2net.dk...
| 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
|
|
Author
19 May 2006 7:44 AM
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

Show quote
"William Stacey [MVP]" <william.sta***@gmail.com> wrote in message
news:ep8VDIveGHA.4912@TK2MSFTNGP05.phx.gbl...
> Why not use two sockets??
>
> --
> William Stacey [MVP]
>
> "Robin Theilade" <nospam.robin.theilade.nospam@nospam.gmail.com> wrote in
> message news:DqWag.37$vJ7.14@news.get2net.dk...
> | 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
> |
> |
>
>

AddThis Social Bookmark Button