错误:将套接字与地址绑定时地址已在使用中,但端口号被`netstat`显示为空闲 [英] Error: Address already in use while binding socket with address but the port number is shown free by `netstat`

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

问题描述

我试图在端口号 8000 绑定我的套接字(服务器套接字).它为我工作并完成了工作.在代码的末尾,我也关闭了套接字.下一刻我再次运行我的代码,它告诉我地址​​已经被使用.我已经打印了错误值的含义 strerror(errno); 以查看我的代码是否在每个点都能正常工作.为了检查端口是否空闲,我使用 netstat 检查了它,但它显示端口号 8000 是空闲的.它在我身上发生了很多次.每次我再等几秒钟,然后它又开始工作.我用的是c语言.那么我的操作系统出现这种行为的原因是什么.

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$ 

推荐答案

我也遇到了同样的问题.这是因为您正在关闭与套接字的连接,而不是套接字本身.socket 可以进入 TIME_WAIT 状态(为了确保所有数据都已经传输完毕,TCP 尽可能保证交付) 最多需要 4 分钟才能发布.

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

这当然很烦人,但这不是错误.请参阅@Vereb 对以下有关 SO_REUSEADDR 使用的答案的评论.

It's certainly annoying, but it's not a bug. See the comment from @Vereb on this answer below on the use of SO_REUSEADDR.

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

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