0

I'm trying to implement a basic connection between two machines on the same local network (client and server), with this code:

def receive():
    SERVER_HOST = "0.0.0.0"
    SERVER_PORT = 5001
    BUFFER_SIZE = 4096
    SEPARATOR = "<SEPARATOR>"

    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.bind((SERVER_HOST, SERVER_PORT))
    s.listen(10)
    print(f"[*] Listening as {SERVER_HOST}:{SERVER_PORT}")
    print("Waiting for the client to connect... ")
    client_socket, address = s.accept()

    print(f"[+] {address} is connected.")
    received = client_socket.recv(BUFFER_SIZE).decode()
def send():
    SEPARATOR = "<SEPARATOR>"
    BUFFER_SIZE = 4096

    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect(("192.168.0.28", 5001))
    print("[+] Connected to ", "192.168.0.28")

From my understanding, as the server's socket is binded to 0.0.0.0 it should listen for connections from all IPs, and the client should be able to connect to this server and send messages to it.

However the client is unable to connect with the server, timing out. When turning off the firewalls on my machines it throws a "ConnectionRefusedError: [Errno 111] Connection refused"

Addtionally when running netstat -a on the server, the command shows that the computer is listening on 127.0.0.1:5001, does this mean the socket is not binding correctly to 0.0.0.0?

I've ensured that I can ping both machines with the IP addresses that I am using, and I've tried various firewall troubleshooting methods (Turning off, creating whitelist rules)

Any help would be appreciated!

2
  • I'm no networking expert but I believe 0.0.0.0 and 127.0.0.1 are equivalent. They both represent the localhost. Commented Oct 28, 2023 at 0:05
  • 1
    @Evyn Only 127.0.0.1 represents localhost, as it is the local loopback IP. 0.0.0.0 on the other hand is a wildcard in the server context, it will bind to all of the machine's local IPs, including the LAN IP that other machines on the same LAN can connect to. Commented Oct 28, 2023 at 0:07

1 Answer 1

1

Per the Python socket documentation:

https://docs.python.org/3/library/socket.html

A pair (host, port) is used for the AF_INET address family, where host is a string representing either a hostname in internet domain notation like 'daring.cwi.nl' or an IPv4 address like '100.50.200.5', and port is an integer.

For IPv4 addresses, two special forms are accepted instead of a host address: '' represents INADDR_ANY, which is used to bind to all interfaces, and the string '<broadcast>' represents INADDR_BROADCAST. This behavior is not compatible with IPv6, therefore, you may want to avoid these if you intend to support IPv6 with your Python programs.

So, change your SERVER_HOST from '0.0.0.0' to ''. This is even demonstrated in the same documentation:

# Echo server program
import socket

HOST = ''                 # Symbolic name meaning all available interfaces
PORT = 50007              # Arbitrary non-privileged port
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    s.bind((HOST, PORT))
    s.listen(1)
    conn, addr = s.accept()
    with conn:
        print('Connected by', addr)
        while True:
            data = conn.recv(1024)
            if not data: break
            conn.sendall(data)
Sign up to request clarification or add additional context in comments.

2 Comments

I've tried changing the '0.0.0.0' to '' and it still seems to be listening on 127.0.0.1 and the client is still returned "ConnectionRefusedError", any ideas?
@jwoo271 I dont use Python myself. If it is behaving as you described, that would be a serious bug and you should contact the author for support.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.