windows 在同一端口上发送和接收 udp 数据包

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5764929/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 16:42:22  来源:igfitidea点击:

sending and receiving udp packets on same port

c++cwindowsudpwinsock

提问by Michael

i need my client app to be able to sendto() and recvfrom() through the same port using UDP.

我需要我的客户端应用程序能够使用 UDP 通过同一个端口发送 to() 和 recvfrom()。

I have discovered i can only bind one socket to the port and it's better to do it when using recvfrom(), otherwise things mess up.

我发现我只能将一个套接字绑定到端口,最好在使用 recvfrom() 时这样做,否则事情会变得一团糟。

I want to be able to still send UDP packets through that port but binding it to a different socket is a problem as i mentioned above.

我希望仍然能够通过该端口发送 UDP 数据包,但如上所述,将其绑定到不同的套接字是一个问题。

What can i do ?

我能做什么 ?

thanks.

谢谢。

回答by Guy Sirton

This should work fine. One socket bound to a port where you use recvfrom() and sendto() on the same socket. See this examplefrom MSDN that shows recvfrom() in isolation - right around the recvfrom() you should be able to sendto() using that same socket.

这应该可以正常工作。一个套接字绑定到您在同一套接字上使用 recvfrom() 和 sendto() 的端口。请参阅MSDN 中的此示例,该示例单独显示了 recvfrom() - 就在 recvfrom() 周围,您应该能够使用同一个套接字发送 to()。