socket 的关闭
来源:互联网 发布:java 中接口interface 编辑:程序博客网 时间:2024/06/03 10:21
用python在写一个东西,用socket监听一个端口,但是重启的时候总是会报错
[ERROR] Address already in use
检查了一下端口
netstat -an | grep 9200
tcp4 0 0 127.0.0.1.9200 127.0.0.1.60481 TIME_WAIT
socket close 之后端口还是会被占用,这个是linux底层的机制,就是防止丢包。
在我的应用场景里面其实不怕丢包,所以把socket定义为可重用就行
http://www.unixguide.net/network/socketfaq/4.5.shtml
What exactly does SO_REUSEADDR do? This socket option tells the kernel that even if this port is busy (in the TIME_WAIT state), go ahead and reuse it anyway. If it is busy, but with another state, you will still get an address already in use error. It is useful if your server has been shut down, and then restarted right away while sockets are still active on its port. You should be aware that if any unexpected data comes in, it may confuse your server, but while this is possible, it is not likely. It has been pointed out that "A socket is a 5 tuple (proto, local addr, local port, remote addr, remote port). SO_REUSEADDR just says that you can reuse local addresses. The 5 tuple still must be unique!" by Michael Hunter (mphunter@qnx.com). This is true, and this is why it is very unlikely that unexpected data will ever be seen by your server. The danger is that such a 5 tuple is still floating around on the net, and while it is bouncing around, a new connection from the same client, on the same system, happens to get the same remote port. This is explained by Richard Stevens in ``2.7 Please explain the TIME_WAIT state.''.
实际代码大概是这样:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)s.bind(('', Command.port))s.listen(5)while True: conn, address = s.accept() data = conn.recv(1024) if not data: pass else: pass
阅读全文
0 0
- socket 的关闭
- 输出流关闭造成的Socket关闭
- 如何优雅的关闭socket
- 关于socket 关闭的问题
- socket输入输出的关闭方式
- 线程关闭socket的方法
- C# Socket的安全关闭
- 关闭Socket
- 关闭Socket
- 关闭socket
- socket关闭
- socket关闭
- socket关闭
- socket关闭
- socket关闭
- 关闭Socket
- 14-socket的实践到内核--socket的关闭
- 14-socket的实践到内核--socket的关闭
- css导航栏选中是有移动下划线的效果
- 20170807ceph入门(一)——ceph中rados对象创建以及文件上传与下载
- 利用python将execl的数据导入hive
- 在 Mac OS X 上安装 TensorFlow
- OpenCV自学笔记24. Raspberry pi +OpenCV 读取摄像头
- socket 的关闭
- 高级for循环(foreach)
- java多线程----关于join方法的注意点,以及守护线程的小结
- Mac环境下Myeclispe2015工具的安装与破解
- PHP 基础—— include 和 require
- hdu1852 Beijing 2008 x^y的因子和2.0
- pandas入门——loc与iloc函数
- 二叉树的静态实现
- Linux 命令全称