1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] Errno 61: Connection refused error when trying to connect to python server

Discussão em 'Python' iniciado por Stack, Julho 14, 2025.

  1. Stack

    Stack Membro Participativo

    I've recently been trying to experiment with creating simple online multiplayer games with python's socket module. 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

    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.

    Continue reading...

Compartilhe esta Página