G
Gotch@
Hi,
I'm new to C# and .NET. I'm working on a server application and I
can't achieve a particular functionality: the server opens a UDP
socket throug UDP Client. This is done in the main thread, that will
use this socket to send UDP Packs to other hosts. On a parallel thread
I need to receive the anwers, but, of course, if I use the socket in
the other thread it goes mad... I think because of some concurrency
issue... in particular, when doing a receive on the UDPClient instance
it throws the following exception: System.Net.Sockets.SocketException,
with message saying something like "Connection closed by remote
host" (can't be more precise cos VS or somebody else translates the
message in Italian, my language) and that's very confusing for a UDP
socket. Anyway I guess it can't be trusted cos I'm not playing fair
with threading. Anyway it is not possible to use locks: the blocking
receive freezes everything.
So the question is: I need a sender socket with port number A, cos the
receiver has to know the port to answer to. I need a receiver socket
with the same port number A, in order to receive the response packets.
If I try to create two sockets bound to the same port number it will
get mad cos it's forbidden. If I try using the same socket (UDPClient
sorry) between the two threads it will get mad again because of race
conditions. Of course a solution would be to insert in the message
itself the address of another port in order to avoid this mess, but
I'd like to avoid this for the following reason: there may be a
situation in which the host running the software may be beside a
firewall, or a NAT. If I use the same port both to send and receive, I
think the router or firewall will leave it open and translate it after
a first packet coming from that port passes by. So if I use different
ports this won't work. I not very sure about this effect, but I
remeber Kazaa, Skype and other apps use it. Moreover seems strange
that's it's not possible to achieve such a simple thing....
Hope you have some clue.
Bye All.
I'm new to C# and .NET. I'm working on a server application and I
can't achieve a particular functionality: the server opens a UDP
socket throug UDP Client. This is done in the main thread, that will
use this socket to send UDP Packs to other hosts. On a parallel thread
I need to receive the anwers, but, of course, if I use the socket in
the other thread it goes mad... I think because of some concurrency
issue... in particular, when doing a receive on the UDPClient instance
it throws the following exception: System.Net.Sockets.SocketException,
with message saying something like "Connection closed by remote
host" (can't be more precise cos VS or somebody else translates the
message in Italian, my language) and that's very confusing for a UDP
socket. Anyway I guess it can't be trusted cos I'm not playing fair
with threading. Anyway it is not possible to use locks: the blocking
receive freezes everything.
So the question is: I need a sender socket with port number A, cos the
receiver has to know the port to answer to. I need a receiver socket
with the same port number A, in order to receive the response packets.
If I try to create two sockets bound to the same port number it will
get mad cos it's forbidden. If I try using the same socket (UDPClient
sorry) between the two threads it will get mad again because of race
conditions. Of course a solution would be to insert in the message
itself the address of another port in order to avoid this mess, but
I'd like to avoid this for the following reason: there may be a
situation in which the host running the software may be beside a
firewall, or a NAT. If I use the same port both to send and receive, I
think the router or firewall will leave it open and translate it after
a first packet coming from that port passes by. So if I use different
ports this won't work. I not very sure about this effect, but I
remeber Kazaa, Skype and other apps use it. Moreover seems strange
that's it's not possible to achieve such a simple thing....
Hope you have some clue.
Bye All.