I tried searching for an answer, but I cannot find an answer that answers my specific problem.
Perhaps I do not know how to formulate the problem correctly.
I think I determined exactly what it is, but I just don't know how to fix it.
EDIT: I tried to use two clients on one TCP Socket. I can’t do it. I have to think about something else. Resolved, I think.
So, I have a
1: Two customers
2: one server
The goal is as follows: The server distributes new user names to all clients as they connect.
This is what happens when I run the program:
Server : identify the host and port, initialize it. Check out
Client 1 : connects to the server. Check out
Client 1 : after connecting, sends a string to the server. Check out
Server Gets a string, checks if a string exists in the list. If it is: Pass, if not, send each new line. Check out
Client 1 : [Pending receipt of data] Receives data, checks to see if the received string matches the one it sent. If so, type (“This is one of ours!”), Otherwise add a new line = in Client Name 2. Check
Client 2: Connecting to the server: check
Server : [If it receives a line, prints it.] (Works) Checks if there is a new line in the list. [This is not] He sends the new username to everyone and then prints ("Sent to all"). Check out
But, when client 2 receives the string, it prints it. However, client 1 never returns a string.
And when I started the client in IDLE, I noticed that something went wrong, as client 1 tried to get the data. (The while loop, in which data = s.recv, started cycling fast instead of waiting)
I asked in the chat, but now there is nobody. I tried to look it, but I really can not find the answer. I suspect that when my server sends a “connection” a second time, it somehow redefines the original client connection .
Here is my server code:
from socket import * import threading import os import csv Username_List = [] host = input("Host: ") port = input("Port: ") ss = socket(AF_INET,SOCK_STREAM) ss.bind((host,int(port))) ss.listen(2) while True: try: connection,address = ss.accept() data = connection.recv(1024) if data: translated_data = data.decode() print(translated_data) if translated_data in Username_List: pass else: Username_List.append(translated_data) connection.sendall(translated_data.encode()) print("Sent new username to everyone") except IOError: connection.close() print("An exception with a connected user occured") break
And here is my client code: [The only difference between client 1 and 2 is changing the username variable]
# Sample Username Client Service Handler. from socket import * import threading import os import csv Username = ("Owatch") host = input("Host: ") port = input("Port: ") try: ss = socket(AF_INET,SOCK_STREAM) ss.connect((host,int(port))) except IOError: print("Aw no man") ss.send(Username.encode()) while True: try: print("Waiting to Recieve Data") data = ss.recv(1024) if data: translated_data = data.decode() print(translated_data) if translated_data == Username: print("It one of ours!") else: Client_Username = translated_data print (Client_Username) except Exception as e: print (vars(e))
If you could help, I would be grateful.
If you know the answer to my question that has already been asked, tell me and I will delete this message so as not to break the rules. Thanks!