Client socket is not receiving data correctly

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!

+4
source share
1 answer

Right after that, I started with what you later changed until what I did worked, created a client class that starts a thread with each connection and adds it to the thread list (please, if I do something terribly wrong people they fix me), the stream works, receives some data checks, if it is in the list of usernames, if it does not send a message to all clients in the list of streams with this name, then the stream simply cools. In any case, on the code.

SERVER !!!

 import csv class client(threading.Thread): Username_List = [] def __init__(self, conn): super(client, self).__init__() self.conn = conn def run(self): print "Client thread started" data = self.conn.recv(1024) print "Received: {0}".format(data) if data in client.Username_List: self.send_msg("Welcome Back!") else: for cnt in threadz: cnt.send_msg(data) print("Sent new username to everyone") client.Username_List.append(data) while True: # dont need nothing now pass def send_msg(self,msg): self.conn.send(msg) host = input("Host: ") port = input("Port: ") ss = socket() #AF_INET,SOCK_STREAM) ss.bind((host,int(port))) print "Server Opening on port: {0}".format(port) ss.listen(2) threadz = [] print "Begining Wait for connections" while True: try: connection, address = ss.accept() print "Got ONE!" c = client(connection) print "Recevied connection from:{0} On port:{1}".format(address[0],address[1]) c.start() threadz.append(c) print "Client appended to threadz, currently {0} threadz active".format(len(threadz)) except IOError,KeyboardInterrupt: connection.close() print("An exception with a connected user occured") break 

CUSTOMER:

 # Sample Username Client Service Handler. from socket import * import threading import os import csv Username = ("ShyGuy") host = input("Host: ") port = input("Port: ") try: ss = socket() #AF_INET,SOCK_STREAM) ss.connect((host,int(port))) #I was using ("localhost",1234) for testing ss.send(Username) except IOError: print("Aw no man") print("Waiting to Recieve Data") while True: try: data = ss.recv(1024) if data: translated_data = data.decode() print(translated_data) if translated_data == Username: print"Name: {0} has been registered on server!".format(translated_data) else: Client_Username = translated_data print "New client name received: {0}".format(Client_Username) except Exception as e: print (vars(e)) 

This works on python 2.7 with two clients locally. You must use the semaphore to stop printing threads simultaneously with the main server: http://en.wikipedia.org/wiki/Semaphore_(programming)

This code doesn’t do anything elegant with disconnecting the client, but as soon as you can handle the exceptions that arise in doing so, I’m sure that you will learn something else.

+1
source

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


All Articles