错误:地址已在使用的同时有地址,但是端口号绑定套接字通过'netstat`显示免费 [英] Error: Address already in use while binding socket with address but the port number is shown free by `netstat`

查看:216
本文介绍了错误:地址已在使用的同时有地址,但是端口号绑定套接字通过'netstat`显示免费的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我在端口号插座(服务器套接字)绑定 8000 。它的工作和做的工作对我来说。在code结束时,我关闭套接字为好。就在第二天瞬间我跑我的$ C $再次C和它显示我的地址已在​​使用。我已经输出了错误值的含义字符串错误(错误); 来看看我的code在每个点工作正常。要检查该端口是免费的我检查使用的netstat ,但它表明,端口号 8000 是免费的。它与我发生了很多次。每次我再等待几秒钟,然后再次开始工作。我使用C语言编写。那么,什么是他的原因这种行为通过我的操作系统。

几个秒钟后,我运行code,然后它的作品。

anirudh @ anirudh-的Aspire-5920:〜/桌面/测试$ sudo的./a.out
创建套接字:成功
打开文件:成功
套接字绑定:地址已在使用
插座听:地址已在使用
^ C
anirudh @ anirudh-的Aspire-5920:〜/桌面/测试$ sudo的netstat的-lntp
活跃​​的互联网连接(仅服务器)
原始的recv-Q发送-Q本地地址外国地址状态的PID /程序名
TCP 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1348 / lighttpd的
TCP 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 984 / sshd的
TCP 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 1131 / cupsd将
TCP 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 1211 / mysqld的
TCP6 0 0 ::: 22 ::: * LISTEN 984 / sshd的
TCP6 0 0 :: 1:631 * ::: LISTEN 1131 / cupsd将
anirudh @ anirudh-的Aspire-5920:〜/桌面/测试$ sudo的./a.out
创建套接字:成功
打开文件:成功
套接字绑定:地址已在使用
插座听:地址已在使用
^ C
anirudh @ anirudh-的Aspire-5920:〜/桌面/测试$


解决方案

我碰到了同样的问题,以及。这是因为您要关闭插座上的连接,而不是插座本身。插座可以进入TIME_WAIT状态(以确保所有数据已传输,TCP保证交付如果可能的话)<一个href=\"http://www.softlab.ntua.gr/facilities/documentation/unix/unix-socket-faq/unix-socket-faq-4.html#ss4.2\"相对=nofollow>,并采取了4分钟释放。

或者,一个真正详细的/技术的解释,<一个href=\"http://www.softlab.ntua.gr/facilities/documentation/unix/unix-socket-faq/unix-socket-faq-2.html#time_wait\"相对=nofollow>检查此链接

它可以是恼人的是肯定的,但有它周围没有真正的方法,它是不是一个错误。

I tried to bind my socket(server socket) at port number 8000. It worked and did the job for me. At the end of the code I close the socket as well. The very next instant I run my code again and it shows me that the address is already in use. I have printed the meaning of error values strerror(errno); to see if my code working properly at each point. To check if the port is free I checked it using netstat but it shows that port number 8000 is free. It has happened with me a lot of times. Every time I then wait for a few more secs and then it starts working again. I am using c language. So what is he reason for this behavior by my OS.

After a few more secs I run the code and then it works.

anirudh@anirudh-Aspire-5920:~/Desktop/testing$ sudo ./a.out 
Socket Creation: Success
File open: Success
Socket Bind: Address already in use
Socket Listen: Address already in use
^C
anirudh@anirudh-Aspire-5920:~/Desktop/testing$ sudo netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      1348/lighttpd   
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      984/sshd        
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      1131/cupsd      
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      1211/mysqld     
tcp6       0      0 :::22                   :::*                    LISTEN      984/sshd        
tcp6       0      0 ::1:631                 :::*                    LISTEN      1131/cupsd      
anirudh@anirudh-Aspire-5920:~/Desktop/testing$ sudo ./a.out 
Socket Creation: Success
File open: Success
Socket Bind: Address already in use
Socket Listen: Address already in use
^C
anirudh@anirudh-Aspire-5920:~/Desktop/testing$ 

解决方案

I've run into that same issue as well. It's because you're closing your connection to the socket, but not the socket itself. The socket can enter a TIME_WAIT state (to ensure all data has been transmitted, TCP guarantees delivery if possible) and take up to 4 minutes to release.

or, for a REALLY detailed/technical explanation, check this link

It can be annoying to be sure, but there's no real way around it and it's not a bug.

这篇关于错误:地址已在使用的同时有地址,但是端口号绑定套接字通过'netstat`显示免费的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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