1

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.

server.py

    import socket

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

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

client.py

    import socket
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    host = '192.168.0.10'
    port = 5000
    s.connect((host, port))
    print(s.recv(1024))
    s.close()

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.

3
  • 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

3

Your host code is

host = `192.168.0.1`

while the client code is

host = '192.168.0.10'

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

Your Answer

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

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