How can I have multiple clients on a TCP Python Chat Server?

Any help on how I can get this to take more than one client, and why not now? Thanks!

Also, is there something I am doing wrong with this code? I did mostly Python 2 tutorials because I can't find them for Python 3.4

Here is my server code:

import socket import time import os from threading import Thread folderPath = "Chat Logs" filePath = folderPath + "/" + str(time.strftime("%H-%M-%S_%d-%m-%Y")) + ".txt" def clientHandler(c): while True: data = c.recv(1024) if not data: break data = data.decode("UTF-8") message = str(data[:data.index("§")]) nick = str(data[data.index("§")+1:]) print(nick + ": " + message) saveChat(nick, message) print(" Sending: " + data) c.send(bytes(data, "UTF-8")) c.close() def saveChat(nick, message): if not os.path.exists(folderPath): os.makedirs(folderPath) if not os.path.exists(filePath): f = open(filePath, "a") f.close() f = open(filePath, "a") f.write(nick + ": " + message + "\n") f.close() def Main(): host = str(socket.gethostbyname(socket.gethostname())) port = 5000 print(host + ":" + str(port) + "\n") Clients = int(input("Clients: ")) s = socket.socket() s.bind((host, port)) s.listen(Clients) for i in range(Clients): c, addr = s.accept() print("Connection from: " + str(addr)) Thread(target=clientHandler(c)).start() s.close() if __name__ == "__main__": Main() 

And here is my client code:

 import socket def Main(): print("Send 'q' to exit\n") address = str(input("ip:port -> ")) nick = input("nick: ") try: if address.index(":") != 0: host = address[:address.index(":")] port = int(address[address.index(":")+1:]) except ValueError: host = address port = 5000 s = socket.socket() s.connect((host, port)) message = input("-> ") while message != "q": s.send(bytes(message + "ยง" + nick, "UTF-8")) data = s.recv(1024) data = data.decode("UTF-8") data2 = data messageServer = str(data[:data.index("ยง")]) nickServer = str(data[data.index("ยง")+1:]) if not data == data2: print(nickServer + ": " + messageServer) message = input("-> ") s.close() if __name__ == "__main__": Main() 
+6
source share
2 answers

First of all, I found these guides very useful: BinaryTides

Here is an example of a simple tcp server that accepts multiple clients. All this receives data from the client and returns "OK .." + the_data. However, you can easily change it to have a function that transmits data (chat msg) to all connected clients. This example uses streaming processing. You must google for the select module. As for your threads, are you sure that: a) using the correct module / method for the job, and b) that you are calling it in the right way?

 import socket import sys from thread import start_new_thread HOST = '' # all availabe interfaces PORT = 9999 # arbitrary non privileged port try: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) except socket.error, msg: print("Could not create socket. Error Code: ", str(msg[0]), "Error: ", msg[1]) sys.exit(0) print("[-] Socket Created") # bind socket try: s.bind((HOST, PORT)) print("[-] Socket Bound to port " + str(PORT)) except socket.error, msg: print("Bind Failed. Error Code: {} Error: {}".format(str(msg[0]), msg[1])) sys.exit() s.listen(10) print("Listening...") # The code below is what you're looking for ############ def client_thread(conn): conn.send("Welcome to the Server. Type messages and press enter to send.\n") while True: data = conn.recv(1024) if not data: break reply = "OK . . " + data conn.sendall(reply) conn.close() while True: # blocking call, waits to accept a connection conn, addr = s.accept() print("[-] Connected to " + addr[0] + ":" + str(addr[1])) start_new_thread(client_thread, (conn,)) s.close() 
+9
source

Check out: http://etutorials.org/Programming/Python+tutorial/Part+IV+Network+and+Web+Programming/Chapter+19.+Sockets+and+Server-Side+Network+Protocol+Modules/19.3+Event -Driven + Socket + Programs / . Example 19-6 (one who has a select system call), as the welcome world of chat applications. You can also take a look at http://beej.us/guide/bgnet/output/html/multipage/index.html for deeper networks with a lower level (C).

+3
source

Source: https://habr.com/ru/post/976918/


All Articles