1

i wanted to make application like a messenger, so I followed this tutorial. I just wanted to try it, and if it work, ill continue with making it better. I did everything i should and it worked.. but, only in localhost.. Is there way, to make it like.. create server on my ip and then for example, my friend will join on it and we can type together ?

This is my server code:

import threading
import socket

host = '127.0.0.1'
port = 55555

server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((host, port))
server.listen()

clients = []
nicknames = []

def broadcast(message):
    for client in clients:
        client.send(message)

def handle(client):
    while True:
        try:
            message = client.recv(1024)
            broadcast(message)
        except:
            index = clients.index(client)
            clients.remove(client)
            client.close()
            nickname = nicknames[index]
            broadcast(f'{nickname} left conversation'.encode('ascii'))
            nicknames.remove(nickname)
            break

def receive():
    while True:
        client, adress = server.accept()
        print(f"you connected to {str(adress)}")

        client.send('NICK'.encode('ascii'))
        nickname = client.recv(1024).decode('ascii')
        nicknames.append(nickname)
        clients.append(client)

        print(f'yur nickname is {nickname}')
        broadcast(f'{nickname} joined!'.encode('ascii'))
        client.send('connected!'.encode('ascii'))

        thread = threading.Thread(target=handle, args=(client,))

        thread.start()
receive()

  

And this is my client code :

import threading
import socket

nickname = input("How did they call you in kindergarden ?: ")

client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(('127.0.0.1', 55555))

def receive():
    while True:
        try:
            message = client.recv(1024).decode('ascii')
            if message == 'NICK':
                client.send(nickname.encode('ascii'))
            else:
                print(message)
        except:
            print("Theres an error.. lets find it together !")
            client.close()
            break

def write():
    while True:
        message = f'{nickname}: {input("")} '
        client.send(message.encode('ascii'))

receive_thread = threading.Thread(target=receive)
receive_thread.start()

write_thread = threading.Thread(target=write)
write_thread.start()

I want to make it in tkinter, so there will be label with your ip. You will have 2 application. First server and second client. If you start server and then client, you will join with client on that ip and then your friends could join too... is there that way ?

4
  • You'll want to bind to the unspecified address 0.0.0.0 that effectively listens on all local IP addresses. However, your programming question is explicitly off-topic here, see the help center. You could probably try on Stack Overflow. – Zac67 Dec 20 '20 at 12:44
  • @Zac67 I already tried that on Stack, they said i should try it here – yakubiq Dec 20 '20 at 14:04
  • If your question is really about remotely accessing a server in a private network (possibly using port forwarding/destination NAT) you need to refine your question and add details about your network (diagram, router/firewall models, configs). Note that consumer-grade devices and home networking are explicitly off-topic here. – Zac67 Dec 20 '20 at 14:19
  • Unfortunately, questions about home networking, consumer-grade devices, applications, and hosts/servers are all off-topic here, and you can ask about those on Super User. Also, programming questions are off-topic here, and you can ask about those on Stack Overflow. – Ron Maupin Dec 21 '20 at 23:47

Browse other questions tagged or ask your own question.