当前位置:  技术问答>linux和unix

如何才能让刚关闭的套接字重新使用,我用SetSockOpt函数不好用啊?

    来源: 互联网  发布时间:2015-08-25

    本文导语:  sockfd=socket(AF_INET,SOCK_STREAM,0); setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&opt,&len); 问题出哪里啊? | 估计是你去监听同一个地址的同一个端口 下面这个是对SO_REUSEADDR和SO_REUSEPORT的详细说明,希望有所帮助...

sockfd=socket(AF_INET,SOCK_STREAM,0);
setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&opt,&len);
问题出哪里啊?

|
估计是你去监听同一个地址的同一个端口
下面这个是对SO_REUSEADDR和SO_REUSEPORT的详细说明,希望有所帮助
SO_REUSEADDR and SO_REUSEPORT Socket Options
The SO_REUSEADDR socket option serves four different purposes:

SO_REUSEADDR allows a listening server to start and bind its well-known port, even if previously established connections exist that use this port as their local port. This condition is typically encountered as follows:

A listening server is started.

A connection request arrives and a child process is spawned to handle that client.

The listening server terminates, but the child continues to service the client on the existing connection.

The listening server is restarted.

By default, when the listening server is restarted in (d) by calling socket, bind, and listen, the call to bind fails because the listening server is trying to bind a port that is part of an existing connection (the one being handled by the previously spawned child). But if the server sets the SO_REUSEADDR socket option between the calls to socket and bind, the latter function will succeed. All TCP servers should specify this socket option to allow the server to be restarted in this situation.

This scenario is one of the most frequently asked questions on USENET.

SO_REUSEADDR allows a new server to be started on the same port as an existing server that is bound to the wildcard address, as long as each instance binds a different local IP address. This is common for a site hosting multiple HTTP servers using the IP alias technique (Section A.4). Assume the local host's primary IP address is 198.69.10.2 but it has two aliases: 198.69.10.128 and 198.69.10.129. Three HTTP servers are started. The first HTTP server would call bind with the wildcard as the local IP address and a local port of 80 (the well-known port for HTTP). The second server would call bind with a local IP address of 198.69.10.128 and a local port of 80. But, this second call to bind fails unless SO_REUSEADDR is set before the call. The third server would bind 198.69.10.129 and port 80. Again, SO_REUSEADDR is required for this final call to succeed. Assuming SO_REUSEADDR is set and the three servers are started, incoming TCP connection requests with a destination IP address of 198.69.10.128 and a destination port of 80 are delivered to the second server, incoming requests with a destination IP address of 198.69.10.129 and a destination port of 80 are delivered to the third server, and all other TCP connection requests with a destination port of 80 are delivered to the first server. This "default" server handles requests destined for 198.69.10.2 in addition to any other IP aliases that the host may have configured. The wildcard means "everything that doesn't have a better (more specific) match." Note that this scenario of allowing multiple servers for a given service is handled automatically if the server always sets the SO_REUSEADDR socket option (as we recommend).

With TCP, we are never able to start multiple servers that bind the same IP address and the same port: a completely duplicate binding. That is, we cannot start one server that binds 198.69.10.2 port 80 and start another that also binds 198.69.10.2 port 80, even if we set the SO_REUSEADDR socket option for the second server.

For security reasons, some operating systems prevent any "more specific" bind to a port that is already bound to the wildcard address, that is, the series of binds described here would not work with or without SO_REUSEADDR. On such a system, the server that performs the wildcard bind must be started last. This is to avoid the problem of a rogue server binding to an IP address and port that are being served already by a system service and intercepting legitimate requests. This is a particular problem for NFS, which generally does not use a privileged port.

SO_REUSEADDR allows a single process to bind the same port to multiple sockets, as long as each bind specifies a different local IP address. This is common for UDP servers that need to know the destination IP address of client requests on systems that do not provide the IP_RECVDSTADDR socket option. This technique is normally not used with TCP servers since a TCP server can always determine the destination IP address by calling getsockname after the connection is established. However, a TCP server wishing to serve connections to some, but not all, addresses belonging to a multihomed host should use this technique.

SO_REUSEADDR allows completely duplicate bindings: a bind of an IP address and port, when that same IP address and port are already bound to another socket, if the transport protocol supports it. Normally this feature is supported only for UDP sockets.



欢迎访问我的个人网站 www.linuxc.net

    
 
 

您可能感兴趣的文章:

 
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。












  • 相关文章推荐
  • 半路出家转行学编程如何才能成为大牛?
  • 修改了hosts.allow,怎么才能生效,是启动了哪个服务才能生效吗?
  • 请较:用window2000做网关,现在LINUX怎么设置才能才能通过它上网呢?
  • 在applet中怎么才能得到当前页面的接口,我的意思是说怎样才能改变比如说一个输入框中的内容
  • 请问xvfs是什么文件系统?怎样才能让linux认出?
  • 如何才能再次进入linux?
  • 提示文字怎样才能按多行显示?
  • 如何才能在java(vj环境下)播放声音?
  • 急!怎样才能获得javax.rmi包?
  • 用什么软件才能打开后缀为.z的文件?
  • 为什么Component的组件要add后才能paint()?
  • 我怎么才能知道一个线程已经执行结束?
  • 请教各位高手一个简单的问题:在JAVA 中如何才能取得一些系统信息?
  • 怎样才能做出半透明的JMenuItem,有人会吗?
  • 我是一个JAVA初学者,不知道怎么才能进阶,大侠给点提示吧!!
  • 我怎样才能找到例子?
  • 请问怎样才能看到JavaScript原来的脚本语言?
  • 怎样才能取出JTextPane中某一行的文本呢?
  • Tomcat中怎樣才能設置session的過期時間?
  • 怎么样才能封住一些端口,不让别的计算机访问
  • 我怎么样才能检测到系统在发包和收包,及数量统计。


  • 站内导航:


    特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

    ©2012-2021,,E-mail:www_#163.com(请将#改为@)

    浙ICP备11055608号-3