Python3.6, OSError: [Errno 57] 套接字未连接 [英] Python3.6, OSError: [Errno 57] Socket is not connected

查看:99
本文介绍了Python3.6, OSError: [Errno 57] 套接字未连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在 python 中编写一个像 UDP 这样的协议,其中包括一些细节,比如 Three_handshake,但我无法运行 server.py,因为它告诉我这样的:

回溯(最近一次调用最后一次):文件/Users/suiyoucheng/PycharmProjects/9331 ASS/receiver.py",行38,在<模块>BYTE_fh, senderAddress = receiverSocket.recvfrom(1024)OSError: [Errno 57] 套接字未连接

和我的 server.py 代码如下所示:

尝试:接收器套接字 = 套接字(AF_INET,SOCK_STREAM)除了:print("创建接收器套接字失败.")系统退出()接收端口 = 2000尝试:receiverSocket.bind(('', receivePort))除了:print("绑定失败.")系统退出()#第一次握手#接收者_ISN = 0接收者_ITIME = time.time()BYTE_fh, senderAddress = receiverSocket.recvfrom(1024) #**哪里出错了**#first_hand = pickle.loads(BYTE_fh)

你们能告诉我如何解决这个问题吗?非常感谢.

解决方案

您需要先开始监听连接,然后才能从套接字读取数据.请在bind() 之后添加receiverSocket.listen() 和receiverSocket.accept().您也可以使用 socketserver 而不是使用原始套接字.>

创建服务器需要采取的总体步骤(无异常处理):

srvsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)server_address = (socket.gethostname(), 25000)srvsock.bind(server_address)srvsock.listen(5)连接,client_address = srvsock.accept()数据 = connection.recv(1024)

I was trying to write a protocol like UDP in python, which include some details like three_handshake, but I can't run the server.py cause it told me like this:

Traceback (most recent call last):
File "/Users/suiyoucheng/PycharmProjects/9331 ASS/receiver.py", line 
38, in <module>
BYTE_fh, senderAddress = receiverSocket.recvfrom(1024)
OSError: [Errno 57] Socket is not connected

and my server.py code shown as below:

try:
    receiverSocket = socket(AF_INET, SOCK_STREAM)
except:
    print("Failed to create receiver socket.")
    sys.exit()


receivePort = 2000
try:
    receiverSocket.bind(('', receivePort))
except:
    print("Bind failed.")
    sys.exit()

#                           First Hand Shake                              #

receiver_ISN = 0
receiver_ITIME = time.time()


BYTE_fh, senderAddress = receiverSocket.recvfrom(1024) #**where I got wrong**#
first_hand = pickle.loads(BYTE_fh)

Could you guys tell me how to fix that? Thank you so much.

解决方案

You need to start listening for connections before you can read the data from the socket. Please add receiverSocket.listen() and receiverSocket.accept() after the bind(). Rather than using raw socket, you can use the socketserver as well.

The overall steps that need to be taken for creating a server (without exception handling):

srvsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_address = (socket.gethostname(), 25000)
srvsock.bind(server_address)
srvsock.listen(5)
connection, client_address = srvsock.accept()
data = connection.recv(1024)

这篇关于Python3.6, OSError: [Errno 57] 套接字未连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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