Python服务器客户端WinError 10057 [英] Python Server Client WinError 10057
问题描述
我正在使用socket
模块在Python 3.3中创建服务器和客户端.我的服务器代码运行正常,但是此客户端代码返回错误.这是代码:
I'm making a server and client in Python 3.3 using the socket
module. My server code is working fine, but this client code is returning an error. Here's the code:
import socket
import sys
import os
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_address = ('192.168.1.5', 4242)
sock.bind(server_address)
while True:
command = sock.recv(1024)
try:
os.system(command)
sock.send("yes")
except:
sock.send("no")
这是错误:
Error in line: command = sock.recv(1024)
OSError: [WinError 10057] A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied
到底是怎么回事?
推荐答案
您似乎对实际上何时连接到服务器感到困惑,因此请记住始终始终先绑定到本地端口.因此,此行:
It looks like you're confused about when you actually connect to the server, so just remember that you always bind to a local port first. Therefore, this line:
server_address = ('192.168.1.5', 4242)
应实际阅读:
server_address = ('', 4242)
然后,在无限循环之前,放入以下代码行:
Then, before your infinite loop, put in the following line of code:
sock.connect(('192.168.1.5', 4242))
您应该很好.祝你好运!
And you should be good to go. Good luck!
我想我应该对术语始终"保持谨慎.在这种情况下,您要先绑定到本地套接字.
I suppose I should be more careful with the term "always." In this case, you want to bind to a local socket first.
这篇关于Python服务器客户端WinError 10057的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!