Home > Enterprise >  Python - [Errno 111] Connection refused on client side of the connection
Python - [Errno 111] Connection refused on client side of the connection

Time:11-29

I'm trying to create a chat between client and server written in Python, using SSL protocols with mutual authentication (i.e: server authenticates client and client authenticates server using certificates). My host machine is being used as the server, and my laptop is the client.

When attempting to connect to my host ip, I keep getting this error on my laptop:

Traceback (most recent call last):
  File "/home/icarus/Codes/RealtimeChat/Chat.py", line 88, in <module>
    main()
  File "/home/icarus/Codes/RealtimeChat/Chat.py", line 75, in main
    connection(ip, port, SSLSock)
  File "/home/icarus/Codes/RealtimeChat/Chat.py", line 35, in connection
    sock.connect((ip, port))
  File "/usr/lib/python3.10/ssl.py", line 1375, in connect
    self._real_connect(addr, False)
  File "/usr/lib/python3.10/ssl.py", line 1362, in _real_connect
    super().connect(addr)
ConnectionRefusedError: [Errno 111] Connection refused

And in the server - which was supposed to print a message saying that a connection was refused - nothing happens, it keeps listening for connections as if nothing happened

Connection function on client side:

 
def connection(ip, port, sock):
    try:
        sock.connect((ip, port))
        print(f"Connected with {ip}")
    except Exception as e:
        print("Connection failed: ", e)
        sock.close()

Server side:

def acceptConnection(self):
        while True:
            con, senderIP = self.sock.accept()
            # Attempting to wrap connection with SSL socket
            try:
                SSLSock = self.getSSLSocket(con)
            # If exception occurs, close socket and continue listening
            except Exception as e:
                print("Connection refused: ", e)
                con.close()
                continue
            print(f"{senderIP} connected to the server")
            # Adding connection to clients list
            self.clients.append(SSLSock)
            # Initializing thread to receive and communicate messages
            # to all clients
            threading.Thread(target=self.clientCommunication, args=(SSLSock, ), daemon=True).start()
 

This is the main function on my server:

def main():
    serverIP = "127.0.0.1"
    port = int(input("Port to listen for connections: "))
    server = Server()
    server.bindSocket(serverIP, port)
    server.socketListen(2)
    server.acceptConnection()

Everything works fine when I connect from my localhost (e.g I open a server on my host machine on one terminal and use another one on the same machine to connect to it). Both machines have the required certificates to authenticate each other, so I don't think that's the problem. Also, without the SSL implementation, the connection between this two different computers was refused by the server

I've tried using sock.bind('', port) on server side, disabling my firewall, used telnet 127.0.0.1 54321 (on my host machine) to check if the connection was working on the specified port (and it is), and also on the client machine (which showed that the connection was refused). I also tried running both scripts with admin privileges (sudo), but it also didn't work. Any suggestions?

CodePudding user response:

I found what was wrong: I was trying to connect to my public IP address (which I found by searching for "What is my ip" on Google), but instead what should be done is to connect to the private IP address (I guess that's the correct name), and you can see yours using ifconfig on Linux and Mac and ipconfig on Windows using a terminal. By doing this, I could connect two computers that are on my network to my desktop server, I still haven't tested for computers in different networks, but the problem has been solved.

  • Related