Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我创建了四个具有不同端口配置的套接字。使用select我从四个文件描述符之一中读取的函数。但是我怎样才能知道我在哪个端口收到了 UDP 数据包,我只有文件描述符,对吧?根据端口,我需要以不同方式处理接收到的数据包。该recv_from函数填充一个struct sockaddr我可以从中读出数据包的源端口,但不能读出目标端口的函数。
select
recv_from
struct sockaddr
在此先感谢您的帮助。
将select所有四个套接字添加到要检查的集合中,select返回时集合将包含活动的套接字。
您现在有两种方法可以确定您从哪个特定套接字/端口返回select:
getsockname
我推荐第一种选择。