1

I wrote a basic server-client scripts using sockets, and everything works fine on my LAN, but it doesnt work when im trying to connect to the client thats not in my LAN. I also port forwarded these ports

this is server

###SERVER####


def ChatConnection():
    print('Waiting for a connection...\n')
    SOCKET = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    SOCKET.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    HOST = ''
    PORT = 8989
    SOCKET.bind((HOST, PORT))
    SOCKET.listen(1)
    CONN, ADDR = SOCKET.accept()
    print('You can now chat')
    while True:
        MSG = str(input('\n[YOU]> '))
        while not MSG: MSG = str(input('\n[YOU]> '))
        CONN.send(MSG.encode())
        print('\nAwaiting reply...')
        REPLY = CONN.recv(4096).decode()
CONN.close()

this is client

###CLIENT###



def ChatConnection():
    print('Waiting for a connection...\n')
    while True:
        try:
            HOST = SERVER EXTERNAL IP
            PORT = 8989
            SOCKET = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            SOCKET.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
            SOCKET.connect((HOST, PORT))
            print('You can now chat with server')
            print('\nWaiting for server...')
            while True:
                REPLY = SOCKET.recv(4096)
                print('\n[USER]> %s'%REPLY.decode())
                MSG = str(input('\n[YOU]> '))
                while not MSG: MSG = str(input('\n[YOU]> '))
                SOCKET.send(MSG.encode())
                print('\nAwaiting reply...')
            SOCKET.close()
    except Exception: pass

what i need to do so this will work on the WAN?

PieThon
  • 11
  • 1
  • 3

1 Answers1

1

Make sure that port forward WORKS, to check this run your program and try running a port scan on your public IP.
Port scanning tool

  • Try to change the HOST to 0.0.0.0.
  • If that didn't work go to CMD and write netstat -a while your script is running.
    Search for the port you are listening on and make sure the IP is 0.0.0.0.
  • If the IP is 0.0.0.0 then try to turn off your Firewall.
Guy Goldenberg
  • 809
  • 7
  • 20