当我在同一个wifi上运行Python的两台计算机之间的progrem时,我得到下一个错误:
Traceback (most recent call last):
File "Server.EX.py", line 10, in <module>
des1 = s.recv(1024)
socket.error: [Errno 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
程序Server.py在一台计算机上运行:
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((///Computer 1 IP///, 1729))
s.listen(1)
conn, address = s.accept()
print("Connection started.Enter 0 to finish connection.")
des1 = 1
while des1 != 0:
des1 = s.recv(1024)
print(des1)
conn.send(raw_input("SERVER: "))
conn.close()
s.close()
Client.py程序在第二台计算机上运行:
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((///Computer 1 IP///, 1729))
print("Connection started. Enter 0 to finish connection.")
des1 = 1
while des1 != 0:
s.send(raw_input("CLIENT: "))
des1 = s.recv(1024)
print(des1)
s.close()
有人可以帮我( - :
*请注意///计算机1 IP ///是实际的IPv4地址
答案 0 :(得分:0)
服务器套接字(server.py中的s
)仅用于接受客户端连接; .accept()
的返回值包括您将用于与该特定客户端进行的所有通信的连接套接字(代码中为conn
)。您正在使用conn
来呼叫.send()
,但却未正确使用s
来呼叫.recv()
。