客户端 UDP 套接字绑定
我正在为 UDP 客户端创建 UDP 套接字,并发送具有不同端口号的 UDP 数据包,并等待来自目的地的回复一段时间。
我的疑问是..是否可以在不关闭套接字的情况下将UDP套接字重新绑定到多个端口号(甚至IP地址)到同一个套接字FD? (我不能使用原始套接字:()
编辑1:我尝试用相同的IP地址和端口号绑定两个不同的套接字文件描述符,但我得到了混合的结果..(对于这两个套接字文件描述符,我是设置 SO_REUSEADDR 选项)。
在 Linux 内核 2.6.8 中
第一个 Socket FD - 绑定成功:
返回错误 98,指出
在 Linux 内核 2.6.24 中。 >
第一个 Socket FD:绑定成功
第二个 Socket FD:绑定成功
I am creating UDP socket for a UDP client and sending UDP packets with different port numbers and wait for the reply from the destination for certain amount of time.
My doubt is .. Is it possible to re-bind a UDP socket to multiple port numbers(even IP-address) to the same socket FD without closing the socket ?? (I cant use RAW sockets :()
EDIT1: I have tried to bind two different socket file descriptors with same IP-Address and Portnumber but I have mixed results .. (For both socket file descriptors I am setting SO_REUSEADDR option).
In Linux kernel 2.6.8
First Socket FD - binds successfully.
Second Socket FD: Returns error 98 saying Address already in use.
In Linux Kernel 2.6.24
First Socket FD: binds successfully
Second Socket FD: binds successfully
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看来 POSIX 现在正式不支持,引用:< em>bind() 函数应将本地套接字地址分配给由描述符套接字标识的套接字,没有分配本地套接字地址。
过去我听说过 re-bind( )在某些平台上是可能的,尽管我个人从未使用过。
保留打开的 UDP 套接字的缓存,将套接字与 poll() 一起使用以进行 IO 多路复用和超时处理。
It appears that POSIX has that now officially as unsupported, quote: The bind() function shall assign a local socket address address to a socket identified by descriptor socket that has no local socket address assigned.
In past I have heard that re-bind()ing was possible on some platforms, though personally I have never used that.
Keep a cache of the open UDP sockets, use the sockets with poll() for the IO multiplexing and time-out handling.
尝试在一组打开的套接字上使用
select()
。Try using
select()
on a group of open sockets.要实现这一点,您可以使用一个 UDP 套接字绑定到一个端口来接收数据,并使用其他端口(绑定到不同的端口)来发送数据。
To achieve this you can use one UDP socket bonud to one port to receive the data, and other (bound to different port) to do the sending.