TCP 和 UDP 套接字可以使用相同的端口吗? [英] Can TCP and UDP sockets use the same port?

查看:45
本文介绍了TCP 和 UDP 套接字可以使用相同的端口吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,在同一台服务器上同时使用UDP和TCP有什么问题吗?

First of all, is there any problem with using both UDP and TCP on the same server?

其次,我可以使用相同的端口号吗?

Secondly, can I use the same port number?

推荐答案

是的,您可以对 TCP 和 UDP 使用相同的端口号.许多协议已经这样做了,例如 DNS 在 udp/53 tcp/53 上工作.

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.

从技术上讲,每个协议的端口池是完全独立的,但对于可以使用 TCP 或 UDP 的更高级别的协议,约定它们默认为相同的端口编号.

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.

在编写您的服务器时,请记住 TCP 套接字的事件序列比 UDP 套接字要困难得多,因为以及普通的 socketbind 调用您还必须listenaccept.

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.

此外,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 accepting connections on the original socket whilst simultaneously servicing multiple clients each of which will be triggering receive events on their own sockets.

这篇关于TCP 和 UDP 套接字可以使用相同的端口吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆