I am working on a simple project where I need two raspberry pi's communicating each other with an ethernet cable. I connect one end of ethernet cable to let's say first pi and the other end to second pi. I am trying to communicate between these two using socket programming.

I started with getting the ip address of the ethernet port at each end using ifconfig. After getting the addresses, I used ping to ping to the ip address from each end and could receive data packets. I started writing socket program for server and client. I followed the code given in the SO link, TCP Communication Raspberry Pi and PC via Ethernet and modified the ip address to my respective pi board's ip address.


    import socket

    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    host = ``
    port = 5000
    s.bind((host, port))

    while True:
        conn, addr = s.accept()
        print("Got connection from ", addr)
        c.send('thank you for connecting')


    import socket
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    host = ''
    port = 5000
    s.connect((host, port))

When I start the server side, everything works fine. Server is created and is listening for communication.

When I start the client side, I get ConnectionRefusedError: [Errno111] connection refused. It happens when the program hits the line `s.connect((host, port))

After some research I came across telnet, though which I tried to establish a connection with the server from client side and to my surprise it was working.

I am stuck at this point and need some help in rectifying this problem. Thanks in advance.

  • 1
    Please don't ask us to guess the code. We need to see it to comment.
    – joan
    Oct 4, 2019 at 5:45
  • Hi @joan, Thanks for replying. I have added the code snippet.
    – TheLazy
    Oct 4, 2019 at 6:11
  • "Connection refused" == There's a system with that IP address but nothing listening on the port. Probably it is the wrong address.
    – goldilocks
    Oct 4, 2019 at 8:15

1 Answer 1


Your host code is

host = ``

while the client code is

host = ''

As you say connecting with telnet works, the address of the host must be, so the client should connect to and not to

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.