Being rather new to socket programming and threading in general, my issue might be stemming from a misunderstanding of how they work. I am trying to create something that acts as both a client and a server using threading.
Following: https://docs.python.org/3/library/socketserver.html#asynchronous-mixins
I created a client class to go with the server and executed both from a main class. The server supposedly launches normally and doesn't give any errors but when I try to connect from the client, it fails on the following:
# In client connection
sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
sock.connect((ip, port))
the error I am getting is:
Traceback (most recent call last):
File "./<project>", line 61, in <module>
client.start_client(serverInfo)
File "/home/<username>/Documents/Github/project/client.py", line 54, in <startclient>
<connectionMethod>(cmd)
File "/home/<username>/Documents/Github/project/client.py", line 112, in <connectionMethod>
sock.connect((remoteHOST,remotePORT))
ConnectionRefusedError: [Errno 111] Connection refused
Even when I modify the server from the code in the referenced python page (just to run on a specific port, 1234), and try to connect to that port with
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
sock.connect(('127.0.0.1',1234))
sock.sendall('Test message')
I get the same problem
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
ConnectionRefusedError: [Errno 111] Connection refused
Why is the server refusing connections? No firewall rules or iptables are in place, running the example that has the client and socket together as is from the site works but even removing the server.shutdown()
line it still kills itself immediately.
Am I missing something?
The behaviour I am expecting is:
./programA
<server starts on port 30000>
./programB
<server starts on port 30001>
<client starts>
--- input/output ---
FROM CLIENT A:
/connect 127.0.0.1 30001
CONNECTED TO 127.0.0.1 30001
ON CLIENT B:
CONNECTION FROM 127.0.0.1 30000
Basically once the clients connect the first time they can communicate with each other by typing into a prompt which just creates another socket targeting the 'remote' IP and PORT to send off the message (instant messaging). The sockets are afterwords left to close because they aren't actually responsible for receiving anything back.
I would appreciate any help I can get on this.