gpt4 book ai didi

python - 我的套接字程序出现连接错误

转载 作者:太空宇宙 更新时间:2023-11-03 16:48:40 26 4
gpt4 key购买 nike

我尝试制作一个连接服务器和客户端的程序。

这是服务器代码:

import socket

HOST="127.0.0.1"
PORT=5000

mySocket=socket.socket(socket.AF_INET, socket.SOCK_STREAM)

try:
mySocket.bind( (HOST, PORT) )

except socket.error:
print ("Call to bind failed")

while 1:
print("Waiting for connection")
mySocket.listen(1)

connection, address=mySocket.accept()
print("Connection recieved from:", address[0])

connection.send("SERVER>>> Connection successful")
clientMessage=connection.recv(1024)

while clientMessage != "Client: end":
if not clientMessage:
break

print (clientMessage)
serverMessage=input("Server: ")
connection.send("Server: "+serverMessage)
clientMessage=connection.recv(1024)

print("Connection ended.")
connection.close()

这是客户端:

import socket
HOST="127.0.0.1"
PORT=5000

print ("Attempting connection")
mySocket=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
mySocket.connect( ( HOST, PORT) )
except socket.error:
print("Call to connect failed")
print ("Connected to server")

serverMessage=mySocket.recv(1024)

while serverMessage!="Server: end":
if not serverMessage:
break

print (serverMessage)
clientMessage=input("Client: ")
mySocket.send("Client: "+clientMessage)
serverMessage=mySocket.recv(1024)

print ("Connection ended.")
mySocket.close()

这是错误:

 File "C:/Python34/python 3.4/client.py", line 13, in <module>
serverMessage=mySocket.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

该错误表明我的地址有问题(?)

我无法理解这个错误想要我做什么。你能帮我吗?

最佳答案

好吧,我对你们的两个程序做了一些编辑。由于您使用的是 python 3,因此您需要将通过套接字作为字节数组发送和接收数据进行处理。下面的客户端和服务器程序使用 python 3.5.1 为我工作。希望这会有所帮助。

此外,我还摆脱了服务器中的 while 1 无限循环,并删除了 try/except 语句,因为我觉得它们没有添加任何内容。如果您要排除 socket.error,您至少应该在此时退出程序,因为这是一个 fatal error 。

服务器.py

import socket

HOST="127.0.0.1"
PORT=5000

mySocket=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
mySocket.bind( (HOST, PORT) )
mySocket.listen(1)
print("Waiting for connection")

connection, address=mySocket.accept()
print("Connection recieved from:", address[0])

connection.send(bytearray("SERVER>>> Connection successful", "utf-8"))
clientMessage=connection.recv(1024)

while clientMessage != "Client: end":
if not clientMessage:
break

print (clientMessage)
serverMessage=input("Server: ")
connection.send(bytearray("Server: "+serverMessage, "utf-8"))
clientMessage=connection.recv(1024)

print("Connection ended.")
connection.close()

客户端.py

import socket
HOST="127.0.0.1"
PORT=5000

print ("Attempting connection")
mySocket=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
mySocket.connect( ( HOST, PORT) )
print ("Connected to server")

serverMessage=mySocket.recv(1024)

while serverMessage!="Server: end":
if not serverMessage:
break

print (serverMessage)
clientMessage=input("Client: ")
mySocket.send(bytearray("Client: "+clientMessage, "utf-8"))
serverMessage=mySocket.recv(1024)

print ("Connection ended.")
mySocket.close()

关于python - 我的套接字程序出现连接错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36083964/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com