0

I have been working on a very simple group chat program. The program works well when tested on the same computer, but doesn't work between different computers on the INTERNET.

I have tried disabling Windows Fire Wall.

I cannot narrow down the code, Sorry. The program uses a socket and Threading library.

Client Code:

import socket
import threading

SERVER_IP = "127.0.0.1"      #This is changed to the servers PUBLIC IP when testing with another computer.
SERVER_PORT = 9279

global name
global sock


def connect():
    global sock
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    server_addres = (SERVER_IP, SERVER_PORT)
    try:
        sock.connect(server_addres)
        print("Connected.")
        return 1
    except:
        print("Cant connect to server.")
        return 0

def send_msg():
    global sock
    while True:
        try:
            msg = name + " >> " + input()
            sock.sendall(msg.encode())
        except Exception as e:
            print(e)

def recieve_msg():
    global sock
    server_active = 1

    while True and server_active:
        try:
            recieved_msg = sock.recv(1024).decode()
            print("\n" + recieved_msg)
        except Exception as e:
            print(e)
            server_active = 0

def main():
    global name

    name = input("Enter name: ")

    connected = connect()

    while True and connected:
        send_thread = threading.Thread(target=send_msg)
        send_thread.start()
        recv_thread = threading.Thread(target=recieve_msg)
        recv_thread.start()
        while recv_thread.is_alive():
            recv_thread.join(timeout=0.1)


    sock.close()

Server code:

import socket
import _thread


host = "My private IP"
port = 9279

global thread_active
thread_active = 1

global client_list

client_list = []

global addr

def on_new_client(clientsocket, pt):
    global thread_active
    global client_list
    while True and thread_active:
        global addr
        try:
            msg = clientsocket.recv(1024).decode()
            print(msg + " " + str(addr))
            for client in client_list:
                client.send(msg.encode())
        except Exception as e:
            print("Client " + str(addr[0]) + ':' + str(addr[1]) + " disconnected (" + str(e) + ")")
            if clientsocket in client_list:
                client_list.remove(clientsocket)
            print(client_list)
            thread_active = 0




s = socket.socket()

print('Server started!')
print('Waiting for clients...')

s.bind((host, port))        # Bind to the port
s.listen(10)                 # Now wait for client connection.

while True:
    c, addr = s.accept()
    print('Got connection from', addr)
    client_list.append(c)
    _thread.start_new_thread(on_new_client, (c, 0))
    thread_active = 1


s.close()
clientsocket.close()
Ron
  • 49
  • 7
  • tried changing the code to `host = "0.0.0.0" in the server file and tried ?? – Panther Sep 28 '19 at 15:58
  • If the computers are on the same network, you need to use the local IP address. If you're trying to communicate across networks (via Internet), you'll need to enable port forwarding on the respective routers. – bkerivan Sep 28 '19 at 16:16
  • ***"between different computers,"***: You can't use `host = "127.0.0.1"`, this is `localhost` == **same computer**. You have to use the `ip` from the `server` you can `ping` from `client`. [Edit] your question and tell **which** `network` `LAN` or `INTERNET`? – stovfl Sep 28 '19 at 18:25
  • @Ron: ***"work on the Internet."***: **1.** Make your `server` world reachable. This requires as @bkervian pointed out `Router` configuration. **2.** Your `client` have to use the `internet address` which your `Router` gets assigned from your provider. Relevant [get-public-external-ip-address](https://stackoverflow.com/questions/3253701/get-public-external-ip-address) – stovfl Sep 28 '19 at 19:12
  • @stovfl I'm just interested in knowing how famous chat applications work if I don't enable port forwarding? And does the client need to enable port forwarding too? – Ron Sep 28 '19 at 19:20
  • @Ron: ***"if I don't enable port forwarding"***: Then the `server` is not reachable from the `client`, **no work**. ***"client need to enable port forwarding too?"***: No, if your `firewall/router` does not restrict your `port = 9279` there is no configuration, beside `SERVER_IP = ""`, required. – stovfl Sep 28 '19 at 19:35

0 Answers0