[英]Errno 61: Connection refused error when trying to connect to python server
I've recently been trying to experiment with creating simple online multiplayer games with python's socket module. 我最近一直在尝试使用python的套接字模块创建简单的在线多人游戏。 I made a first draft of a server and client program, and though they work perfectly when I run them both from the same computer, my attempts to connect the client when running on another computer results in the following error message:
我制作了服务器和客户端程序的初稿,尽管当我在同一台计算机上运行它们时,它们都可以正常工作,但是当我在另一台计算机上运行时尝试连接客户端时,会出现以下错误消息:
Traceback (most recent call last):
File "/Users/Admins2-Admins_In_Space/Downloads/gameclient.py", line 22, in <module>
client.connect((host,port))
ConnectionRefusedError: [Errno 61] Connection refused
(both computers are connected to the same router, so it isn't a problem there.) The code for the server is (两台计算机都连接到同一路由器,因此这不是问题。)服务器代码为
import socket, threading
class dataBase():
"A class to store all playerdata"
def __init__(self):
self.data = []
class client():
"handles an individual client"
def __init__(self,ip,port,value,dataBase):
self.mainThread = threading.Thread(None,self.run)
self.ip = ip
self.port = port
self.value = value
self.dataBase = dataBase
print('New connection with' + ip)
self.mainThread.start()
def run(self):
while True:
data = conn.recv(1024).decode()
if data != None:
exec('data = ' + data)
self.dataBase[self.value] = data
data = self.dataBase
message = []
for d in range(len(data)):
if d == value:
continue
message.append(data[d])
if message != []:
conn.send(str(message).encode())
else:
self.conn.close()
break
if __name__ == '__main__':
data = []
host = '127.0.0.1'
port = 1234
value = 0
threads = []
sock = socket.socket()
sock.bind((host,port))
while True:
sock.listen(5)
(conn,(ip,port)) = sock.accept()
newThread = client(ip,port,value,data)
data.append(())
threads.append(newThread)
value += 1
for t in threads:
t.join()
and here's the client, up to line 22 这是客户,直到第22行
import pygame, socket, sys
from pygame.locals import *
host = '127.0.0.1'
port = 1234
up = False
down = False
left = False
right = False
x = 0
y = 0
data = None
if __name__ == '__main__':
pygame.init()
window = pygame.display.set_mode((1250,1000), 0, 32)
pygame.display.set_caption('client test')
client = socket.socket()
client.connect((host,port))
I've been running the server from a raspberry pi 3 model b with the latest version of raspbian, and the failed client tests have been running on various macs. 我一直在使用树莓派pi 3模型b和最新版本的raspbian运行服务器,并且失败的客户端测试已在各种Mac上运行。
though they work perfectly when I run them both from the same computer, my attempts to connect the client when running on another computer results in the following error …
尽管当我从同一台计算机上运行它们时,它们都能正常工作,但是在另一台计算机上运行时尝试连接客户端会导致以下错误……
In the client, you still have host = '127.0.0.1'
(ie localhost ), but if you want to connect to a server on another computer , you have to set host
to that computer's address. 在客户端中,您仍然具有
host = '127.0.0.1'
(即localhost ),但是如果要连接到另一台计算机上的服务器,则必须将host
设置为该计算机的地址。 And in the server, you should change host = '127.0.0.1'
to host = '0.0.0.0'
. 在服务器中,您应该将
host = '127.0.0.1'
更改为host = '0.0.0.0'
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.