您的位置:首页 > 理论基础 > 计算机网络

TCP 和 UDP 能否使用相同的端口号?

2015-12-11 15:14 507 查看
答案:可以。

参考:

http://stackoverflow.com/questions/6437383/can-tcp-and-udp-sockets-use-the-same-port

Yes, you can use the same port number for both TCP and UDP. Many protocols already do this, for example DNS works on udp/53
and tcp/53.

Technically the port pools for each protocol are completely independent, but for higher level protocols that can use either TCP or UDP it's convention that they default to the same port
number.

When writing your server, bear in mind that the sequence of events for a TCP socket is much harder than for a UDP socket, since as well as the normal
socket
and
bind
calls you also have to
listen
and
accept
.

Furthermore that
accept
call will return a new socket and it's
that socket that you'll then have to also poll for receive events. Your server should be prepared to continue
accept
ing connections on the original socket whilst simultaneously servicing multiple clients each of which will be triggering receive events on their own sockets.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: