C++ UDP RecvFrom, SendTo Different Sockets

家住魔仙堡 提交于 2019-12-11 14:10:10

问题


i wrote a multithreaded UDP server after the following scheme:

Scheme: 1 Receiver Thread (Multiple Worker Threads, each has an own Socket, not bindend ,just created Ipv4, UDP,Datagram) Message gets pushed to 1 worker which then proceed's it and then sends an answere with its own socket.

Problem: This works perfectly on all my own test programs but for some odd reason it doesnt work with an old software for what i am emulating the server. The software uses async Wsa (overlapped), but i still doesn't get why it doesnt work.

Confusion: It works if I use the same socket for sending as i used for receiving the data on the serverside. I dont get why, udp is a connectionless protocol so how can it detect a different socket?


回答1:


Confusion: It works if I use the same socket for sending as i used for receiving the data on the serverside. I dont get why, udp is a connectionless protocol so how can it detect a different socket?

If you look in the UDP headers of the packets you are sending you will notice that they contain a "UDP Source Port" field. That field can be examined by the receiver of the packet (via recvfrom()) to find out which UDP port the sending UDP socket used on the sending machine (note that this is different from the "UDP Destination Port" field that determines which port the packet should be delivered to on the receiving machine). It's possible that in your case, the program you are communicating with is looking at that field and adjusting its behavior based on that field's value.

If you're wondering what that field will be set to if you never called bind() on the sending UDP socket, the answer is that the OS will choose an available UDP port number to send from (essentially an implicit bind()).



来源:https://stackoverflow.com/questions/48245238/c-udp-recvfrom-sendto-different-sockets

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!