Hi, I’ve got a little of a weird problem. I have code of simple client-server communication, one of the computers as client and other as server or the opposite.
While computer 1 can establish connection with computer 2 when he acts as the client, a connection cannot be formed when he is the server. He isn’t accepting any connections, from any other computer. I’m not sure why this happens as the opposite proccess works. if it helps, the codes:
server:
import socket
IP = "0.0.0.0"
PORT = 8820
def main():
server_socket = socket.socket()
server_socket.bind(("0.0.0.0", PORT))
server_socket.listen()
print("server on")
(proxy_socket, proxy_address) = server_socket.accept()
print("client on")
while True:
data = proxy_socket.recv(1024).decode()
data_to_proxy = f"Hi {data}"
proxy_socket.send(data_to_proxy.encode())
if "EXIT" in data_to_proxy:
break
server_socket.close()
proxy_socket.close()
client:
import socket
IP_PROXY = "*.*.*.*"
PORT = 8820
def main():
# Use a breakpoint in the code line below to debug your script.
my_socket = socket.socket()
my_socket.connect((IP_PROXY, PORT))
while True:
user_input = input("Enter your message")
my_socket.send(user_input.encode())
data = my_socket.recv(1024).decode()
print(f"the data is {data}")
if user_input == "EXIT":
break
my_socket.close()
Are the two computers on the same network? For example, the IP addresses 192.168.0.4 and 192.168.0.5 would be on the same network, but if you have 192.168.2.95 and 192.168.0.16 then they’re on different networks, and routing may be a factor here. (That’s an oversimplification but hopefully you know what I mean, or that’s enough to go and research it. Look up “netmask” or “CIDR” to find out more.)
It’s possible that you have a firewall. Personal firewalls often attempt to block unauthorized applications from listening on any socket; real firewalls may very well control exactly which directions connections may be established in. Do you control both computers?
Are other programs able to listen on sockets?
Your code looks pretty fine to me, aside from the fact that nothing is calling main(). Are these running as stand-alone scripts, or are they embedded inside something else?
The networks are identical, I do control both computers, and I’ve been wondering if there is some firewall blocking the connection. Should I disactivate something specific?
Hmm, depends on what sort of systems they are. Personal firewalls tend to be found on Windows systems and include Windows Defender (the one Microsoft ships) and various antivirus and general “protection” programs. However, I can’t advise in specifics since I haven’t used Windows in many many years.
On a Mac, I’m not sure what it’s called, but there’s a built-in thing that usually pops up if you try to listen on a port. You would have to research that yourself and see if it has an option to block without prompting. Again, can’t help with specifics as I don’t own a Mac, but there should be info out there.
On Linux, which I do use, firewalling is more a matter of IP traffic rules. What you’re looking for is iptables or nftables. Try commands like:
(cut-down view of my own firewall; note that there’s a lot of ACCEPT lines for statistical purposes, but generally, this is the sort of info you’d get)
I’m really not familiar with the nftables commands, as they tend to be more low-level and designed for other tools rather than for human consumption, so you’d have to see if you have any sort of firewall application installed.
Note that it’s entirely possible for a firewall to be on EITHER machine, but at least with personal firewalls, it’s more likely to be the one that’s unable to act as a server.