c++ Hole punching UDP(RTP)

放肆的年华 提交于 2019-12-04 16:48:07

There are two possible definitions of "opening a port". One is opening a port with bind() for UDP or listen() for TCP, another one is opening a port in a firewall.

You need to open a port with an API call in order to receive something, there is no way around it, but you probably realize this, so I think you mean opening a port in a firewall. But you don't need to do this on the side that initiates communication (the client). This applies both to TCP and UDP, unless your firewall is set up in a very paranoid mode. Any reasonable firewall would allow a response from a server to a UDP port if there was a datagram sent from this port to the same server some time before. You only need hole punching if both sides are behind NATing firewalls/routers. That's how Skype does it.

Moreover, you don't even have to bother with recvfrom() and such stuff. You can just bind() a UDP socket, then use connect() and recv()/send() or read()/write() exactly in the same way as you'd do with TCP.

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