Python socket.error: [Errno 111] 尝试连接到套接字时

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26558303/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 00:39:22  来源:igfitidea点击:

socket.error: [Errno 111] when trying to connect to a socket

pythonsocketsports

提问by dynamo

I was trying to write a code where a client connects to server on a default port number, the server then sends another port number to the client. The client now connects to the new port number.

我试图编写一个代码,其中客户端通过默认端口号连接到服务器,然后服务器向客户端发送另一个端口号。客户端现在连接到新的端口号。

Client:

客户:

import socket
import sys
import os
import signal
import time
s = socket.socket()
s.connect(("127.0.0.1", 6667))
line = s.recv(1024)
if line.strip():
    port = int(line)
    s.close()
    soc = socket.socket()
    soc.connect(("127.0.0.1", port))
    print soc.recv(1024)
    soc.close()
else:
    s.close()

Server:

服务器:

import socket
import sys
import os
import signal
import time
port = 7777
s = socket.socket()
s.bind(("127.0.0.1", 6667))
s.listen(0)
sc, address = s.accept()
print address
sc.send(str(port))
sc.close()
s.close()
sock = socket.socket()
sock.bind(("127.0.0.1", port))
soc, addr = sock.accept()
print addr
soc.send("Success")
soc.close()
sock.close()

When I execute this code, I am getting following errors on client and server sides.

当我执行此代码时,我在客户端和服务器端收到以下错误。

Server:

服务器:

('127.0.0.1', 36282)
Traceback (most recent call last):
  File "server.py", line 17, in <module>
    soc, addr = sock.accept()
  File "/usr/lib/python2.7/socket.py", line 202, in accept
    sock, addr = self._sock.accept()
socket.error: [Errno 22] Invalid argument

Client:

客户:

Traceback (most recent call last):
  File "client.py", line 13, in <module>
    soc.connect(("127.0.0.1", port))
  File "/usr/lib/python2.7/socket.py", line 224, in meth
    return getattr(self._sock,name)(*args)
socket.error: [Errno 111] Connection refused

Can someone explain me the reason for these errors and provide a solution for these errors.

有人可以向我解释这些错误的原因并为这些错误提供解决方案。

采纳答案by Michael Petch

Before you can listento a TCP/IPsocket (a connection based streaming socket) you need to use bindto assign a socket (created with socket.socket()) . Then you need to do listento prepare it for incoming connections and then finally you do accepton the prepared socket.

在您可以listen使用TCP/IP套接字(基于连接的流套接字)之前,您需要使用bind来分配套接字(使用socket.socket())。然后你需要listen为传入的连接做准备,最后你accept在准备好的套接字上做。

You appear to be missing sock.listen(0)after your call to sock.bind(("127.0.0.1", port)). The Python documentationis short on details but it does say this about TCP/IP:

sock.listen(0)在您致电 后,您似乎失踪了sock.bind(("127.0.0.1", port))。在Python文档很短的细节,但它说,这大约TCP / IP:

Note that a server must performthe sequence socket(), bind(), listen(), accept()(possibly repeating the accept() to service more than one client), while a client only needs the sequence socket(), connect(). Also note that the server does not sendall()/recv() on the socket it is listening on but on the new socket returned by accept().

请注意,服务器必须执行socket()bind()listen()accept()序列(可能重复 accept() 以服务多个客户端),而客户端只需要 socket()、connect 序列()。另请注意,服务器不会在它正在侦听的套接字上发送all()/recv(),而是在accept() 返回的新套接字上发送。

Python bases its socket module on a Berkeley Socket model. You can find some more detailed information on Berkeley Sockets at this link. In particular it says this about bind:

Python 的套接字模块基于伯克利套接字模型。您可以在此链接中找到有关 Berkeley Sockets 的更多详细信息。特别是它说的是bind

bind()assigns a socket to an address. When a socket is created using socket(), it is only given a protocol family, but not assigned an address. This association with an address must be performed with the bind() system call before the socket can accept connectionsto other hosts.

bind()将套接字分配给地址。当使用 socket() 创建套接字时,它只被赋予一个协议族,但没有分配地址。在套接字可以接受与其他主机的连接之前,必须使用 bind() 系统调用执行与地址的这种关联。

Also consider what would happen if your client gets sent a port number (and tries to connect) before the server starts listening for connections (on port 7777 in this case). Although not the cause of your problems, I wanted to point out the scenario for completeness. Something you may consider is not closing the port 6667 socket until after you have called listenon the port 7777 socket. After calling listenyou can then close down the first socket. On the client after reading the port you can wait until the first connection (port 6667) is closed down by the server and then connect to port 7777.

还要考虑如果您的客户端在服务器开始侦听连接(在本例中为端口 7777)之前收到一个端口号(并尝试连接)会发生什么情况。虽然不是您问题的原因,但我想指出场景的完整性。您可能需要考虑的是,在调用listen端口 7777 套接字之前,不要关闭端口 6667套接字。调用后,listen您可以关闭第一个套接字。在客户端读取端口后,您可以等到第一个连接(端口 6667)被服务器关闭,然后再连接到端口 7777。