Reputation:
I've written two programs according to a guide on sockets in python. I'm using a rasbperry pi 3 as a client, and a regular linux ubuntu computer as a server. this is the server software:
import socket
import sys
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_address = ('localhost', 15000)
print("starting up on %s port %s" % server_address, file=sys.stderr)
sock.bind(server_address)
# Listen for incoming connections
sock.listen(1)
while True:
# Wait for a connection
print("waiting for a connection", file=sys.stderr)
connection, client_address = sock.accept()
try:
print("connection from ", client_address, file=sys.stderr)
# Receive the data in small chunks and retransmit it
while True:
data = connection.recv(16)
print("received %s" % data, file=sys.stderr)
if data:
print("sending data back to the client", file=sys.stderr)
connection.sendall(data)
else:
print("no more data from ", client_address, file=sys.stderr)
break
finally:
# Clean up the connection
connection.close()
and this is the client software:
import socket
import sys
# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Connect the socket to the port where the server is listening
server_address = ('192.168.18.250', 15000)
print("connecting to %s port %s" % server_address, file=sys.stderr)
sock.connect(server_address)
try:
# Send data
message = "This is the message. It will be repeated."
print("sending %s" % message, file=sys.stderr)
sock.sendall(message)
# Look for the response
amount_received = 0
amount_expected = len(message)
while amount_received < amount_expected:
data = sock.recv(16)
amount_received += len(data)
print("received %s" % data, file=sys.stderr)
finally:
print("closing socket")
sock.close()
this is the output on the server:
peter@GIGAS:~/thermServer$ python3 thermServer.py
starting up on localhost port 15000
waiting for a connection
and this is the output on the raspberry pi:
pi@raspberrypi:~ $ python3 thermClient.py
connecting to 192.168.18.250 port 15000
Traceback (most recent call last):
File "thermClient.py", line 10, in <module>
sock.connect(server_address)
ConnectionRefusedError: [Errno 111] Connection refused
I have forwarded the port in my router, but as this is internal traffic that shouldn't matter, did I miss adding something in the server that opens the port properly or do I need to fiddle with something outside of the project in my linux machine?
Upvotes: 1
Views: 4083
Reputation: 7634
When you create a socket, bound to the localhost
address, you create a socket listening on the "loopback interface", i.e. 127.0.0.1. You appear to be trying to connect to the server by the local IP - 192.168.18.250, which is assigned to a different interface (generally the one connected to your LAN). Because nothing is listening to port 15000 on that interface you get a connection refused.
You have two solutions here: either change the server to be listening to the correct interface (listening to 0.0.0.0 generally works here, although if it's a guaranteed static IP, use that instead), or change the client to try to connect to the loopback interface - try connect on 127.0.0.1 or "localhost"
Upvotes: 1
Reputation: 5768
In your server code you have:
server_address = ('localhost', 15000)
This sets up a listener on ipnumer 127.0.0.1. This 'localhost' ip cannot be contacted from clients outside of this server.
If you want your server to listen on all assigned ip-adresses, use:
server_address = ('0.0.0.0', 15000)
Upvotes: 3