[英]Socket connection refused after first message
I communicate Python with Matlab via sockets. 我通过套接字与Matlab通信Python。 However, even before going there, I want to test sockets with netcat .
但是,即使在去那里之前,我也想用netcat测试套接字。 So I establish server using
nc -lkp 25771
, and make Python client to send a message to this server: 所以我使用
nc -lkp 25771
建立服务器,并使Python客户端向此服务器发送消息:
import socket
host = 'localhost'
port = 25771
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host,port))
s.send('Hello there')
s.close()
After running python client.py
server prints out 'Hello there'; 运行
python client.py
服务器打印出'Hello there'; however, after I try to run client script one more time it raises exception. 但是,在我尝试再次运行客户端脚本之后,它会引发异常。
Traceback (most recent call last): File "client.py", line 13, in s.connect((host,port)) File "/usr/lib/python2.7/socket.py", line 224, in meth return getattr(self._sock,name)(*args) socket.error: [Errno 111] Connection refused
回溯(最近一次调用最后一次):文件“client.py”,第13行,在s.connect((主机,端口))文件“/usr/lib/python2.7/socket.py”,第224行,在meth return getattr(self._sock,name)(* args)socket.error:[Errno 111]连接被拒绝
Why the same command raises the error second time? 为什么同一命令第二次引发错误? What changes after my first command?
我的第一个命令后有什么变化?
You are using traditional version of netcat (netcat-traditional) which doesn't support -k option. 您正在使用不支持-k选项的传统版本的netcat(netcat-traditional)。 you can confirm checking the man page of your netcat by typing man nc in your terminal .
您可以通过在终端中输入man nc来确认检查netcat的手册页。
Install the netcat-openbsd version using the command sudo apt-get install netcat-openbsd 使用命令sudo apt-get install netcat-openbsd安装netcat-openbsd版本
now switch to netcat-openbsd version using the command 现在使用该命令切换到netcat-openbsd版本
sudo update-alternatives --config nc and choose the netcat-openbsd . sudo update-alternatives --config nc并选择netcat-openbsd。
now you can use nc -lk 25771 . 现在你可以使用nc -lk 25771。 this listens on port 25771 for multiple connections .
这将侦听端口25771上的多个连接。
you can also use the commands discussed here Netcat: using nc -l port_number instead of nc -l -p port_number 你也可以使用这里讨论的命令Netcat:使用nc -l port_number而不是nc -l -p port_number
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.