Fix The Error “Connectionrefusederror: [errno 61] connection refused”

Python programming language doesn’t need an introduction as it is the core or programming world that every programmer, developer, beginner, and programming student is aware of. Different types of applications are being designed for various purposes. Even programmers use Python to code programs for websites. When working on this language, you may experience the error warning “Connectionrefusederror: [errno 61] connection refused”.

Errors always seem odd and annoying whenever we get them popped up. You don’t need to get irritated anymore as we are here to help you fix the error in a simpler way. But, first, you need to check out how you get into the trouble

How the error shows up

When you are working on a chat program, you end with the error warning:

ConnectionRefusedError: [Errno 61] Connection refused

The server code you may implement is as follow

import socket

def socket_create():

    global host
    global port
    global s
    host = ''
    port = 9999
    s = socket.socket()

except socket.error as msg:
    print("Socket creation error" + str(msg))

#Wait for client, Connect socket and port
def socket_bind():
    global host
    global port
    global s
    print("Binding socket to port: " + str(port)) 
    s.bind((host, port))
except socket.error as msg:
    print("Socket binding error" + str(msg) + "\n" + "Retrying...")

#Accept connections (Establishes connection with client) socket has to       be listining
def socket_accept():
   conn, address = s.accept()
   print("Connection is established |" + " IP:" + str(address[0]) + "|    port:" + str(address[1]))

def chat_send(conn):
 while True:
    chat =input()
    if len(str.encode(chat)) > 0:
        client_response = str(conn.recv(1024), "utf-8")
def main():


The client-side code you may use:

import socket

#connects to server
s = socket.socket()
host = ''
port = 9999
s.connect((host, port))

#gets chat
while True:
    data = s.recv(1024)
    print (data[:].decode("utf-8")) 
    chat = input()

It’s exactly how you code and then land up in the error.

Solutions To Fix “Connectionrefusederror: [errno 61] connection refused”

You get the error warning when you try to design a client-server program as the server program can’t connect to the client program. We have ideas to fix the error.

Solution 1 – Using server port

The use of the exact server port, 5000, can fix the error warning. Once you update the code, it is something like the following

import socket

def ClientProgram():
    host = socket.gethostname()
    port = 5000 ## We fixed here.
    ClientSocket = socket.socket()
    ClientSocket.connect((host, port))
    ClientMessage = input(" -> ")
    while ClientMessage.lower().strip() != 'bye':
        ServerMsg = ClientSocket.recv(1024).decode()
        print('Received from server: ' + ServerMsg)
        ClientMessage = input(" -> ")
if __name__ == '__main__':

You get the following output on the client side when you run the client program

-> Hi Server
Received from server: Hi Client
 -> This is a message from the client
Received from server: This is a message from the server

Output on the server side

Connection from: ('', 11418)
from connected user: Hi Server
 -> Hi Client
from connected user: This is a message from the client
 -> This is a message from the server

To fix the error, you need to make sure you execute the server program first and then run the client program.

Solution 2 – Use IP from the server

The client should connect with the server using the same computer, which means a local one having an IP like client needs to use IP from the server (i.e.

In order to keep a check on the server, use the following for Linux and windows operating systems

Linux users

Use the below command in the console


Windows users

In cmd.exe, use the following command


In the case of a different network, the solution will not work for you. You need to set an external IP and set the router, once you set the IP, you can resolve the error. But, setting up the IP is a task to do.


We shed light on the solutions to fix the error “Connectionrefusederror: [errno 61] connection refused”. I hope you enjoyed it.

I wish you happy coding!

Leave a Reply

Your email address will not be published. Required fields are marked *