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.