Share via


Send and receive UDP packets on host machine?

Question

Thursday, September 15, 2016 4:01 AM

I am testing a server and whenever I send UDP packets out, the server receives them as well. The data headers used by the client and server are the same and I do not want to change the design unless absolutely necessary. I assume real servers operate this way when the host and client are on separate machines. The client does not receive any of those packets. Is there a way to solve this problem without using two machines?

All replies (5)

Thursday, September 15, 2016 7:55 AM âś…Answered

Hello,

 Here is an article that describes the HowTo for UDP Client/Server on same system.

http://stackoverflow.com/questions/687868/sending-and-receiving-udp-packets-between-two-programs-on-the-same-computer

 Hope this helps :)


Thursday, September 15, 2016 11:31 AM

Why does the Server receive them as well?

Especially if they run on the same computer, the Client and Server should use different portnumbers. It is just good practice and while we have all those port numbers beyond 1024 to choose from.

As User3DX wrote, there is a way you can make such a pattern (multiple programsm listening to one port). But I still would not consider it a good idea for most cases.
Why would you want your own messages from a UDP connection? Verification that the package left? Then just use TCP to begin with and get proper feedback from the target.

When two process can communicate via the Loopback IP adress, they can do so over any network.
None of the programm side networking code cares if the target is on the same computer, the same switch or the Voyager 2 Probe. Making certain there is a path is a networking problem, not a programming one.

Remember to mark helpfull answers as helpfull and close threads by marking answers.


Thursday, September 15, 2016 2:32 PM

Why does the Server receive them as well?

Especially if they run on the same computer, the Client and Server should use different portnumbers. It is just good practice and while we have all those port numbers beyond 1024 to choose from.

As User3DX wrote, there is a way you can make such a pattern (multiple programsm listening to one port). But I still would not consider it a good idea for most cases.
Why would you want your own messages from a UDP connection? Verification that the package left? Then just use TCP to begin with and get proper feedback from the target.

When two process can communicate via the Loopback IP adress, they can do so over any network.
None of the programm side networking code cares if the target is on the same computer, the same switch or the Voyager 2 Probe. Making certain there is a path is a networking problem, not a programming one.

Remember to mark helpfull answers as helpfull and close threads by marking answers.

Thanks for the replies. The server receiving its own messages was unintentional - it is the problem I was trying to solve essentially. Sorry if I was not clear enough. The client and server seem to be using different ports automatically.

What I did was after a TCP connection was established through a listener server-side, the endpoint of that client was passed into the constructor of the udpclient. Each client on the server has its own "Client" class which stores has its own udpClient. 

Now,

@User3DX I have seen this post before. My worry is that if the server listens for udp messages from any IP, then how is it supposed to distinguish messages between clients? 

Edit: I did try your method though. I get this error: System.Net.Sockets.SocketException (0x80004005): Only one usage of each socket address (protocol/network address/port) is normally permitted


Thursday, September 15, 2016 4:00 PM | 1 vote

Hello,

 You first must comply with Protocal, mean TCP is actively RX/TX while UDP is RX or TX.

Example: Downloading a fairly large file then the Server/Client normally use TCP. But,

for a Chat/Relay then UDP is used.  Have a look at the information in the below link,

should help you understand the differences and when to or not to use.

 https://msdn.microsoft.com/en-us/library/c19ex43h(v=vs.110).aspx

 As for the Server knowing what client send what is upto the Server to track IP address.

I would use collection object type Stack to hold the IP addresses.  The Port allows

client/servers to RX/TX data while keeping the same IP address when using TCP, UDP

or multicast .  Give the link above a good read and you will have better understanding.

 Thanks :)


Friday, September 16, 2016 12:56 AM

Thanks User3DX

My solution was to bind to the port I am receiving on and connect to the port I am sending on. I used separate ports for sending and receiving UDP data.

For example:

Server send port = 8000 = client receive port

Server receive port = 8001 = client send port

I already had a good idea on when to use TCP and UDP. The TCP connection was just used as a handshake between the client and server and to send and receive critical data (guarantee my packets are being sent and received).