简体   繁体   English

C /确定在其上接收到数据包的端口

[英]C / determine port on which a packet was received

I have created four sockets with different port configurations. 我创建了四个具有不同端口配置的套接字。 With the select function I read from one of the four file descriptors. 使用select功能,我从四个文件描述符之一中读取了内容。 But how can I find out on which port I received the UDP packet, I do only have the file descriptor, right? 但是,如何确定在哪个端口上收到UDP数据包,我只有文件描述符,对吗? Depending on the port I need to process received packets differently. 根据端口的不同,我需要对接收到的数据包进行不同的处理。 The recv_from function fills a struct sockaddr from which I can read out the source port of the packet, but not the destination port. recv_from函数填充了一个struct sockaddr ,我可以从中读取数据包的源端口,但不能读取目标端口。

Thanks in advance for your help. 在此先感谢您的帮助。

With select you add all four sockets to the set you want to check, and when select returns the set will contain the active sockets. 使用select ,会将所有四个套接字添加到要检查的集合中,并且在select返回时,集合将包含活动的套接字。

You now have two ways of figuring out which specific socket/port you get back from select : 现在,您可以通过两种方式来确定从select返回的特定套接字/端口:

  1. Compare to the sockets you have. 与您拥有的插槽进行比较。
  2. Get the local port (though getsockname ) and compare to the ports. 获取本地端口(尽管getsockname )并与端口进行比较。

I recommend the first alternative. 我建议第一种选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM