Reputation: 3640
Situation: A server receives connecting clients, and waits for them to make requests.
The Problem: When clients connect, the socket created for them by the server is assigned the variable clientsocket. When a client makes a request, they are removed from a list of clients in the lobby. However, if two clients connect but don't make requests, the last client to connect becomes the variable clientsocket.
Effect of Problem: If the first client that connected decides to make a request, the variable clientsocket is removed from the lobby list. However, clientsocket will not be referring to the first client, but the second (Or last one to connect). So when the second makes a request, an error is generated saying that clientsocket cannot be removed from the lobby because it no longer exists in the lobby list. Meanwhile, client 1 still does.
Here is the snippet of code that does what I am talking about:
rlist,wlist,xlist = select.select([Listeningsocket]+ Connection_List,[],[])
for i in rlist:
if i == Listeningsocket:
clientsocket,address = Listeningsocket.accept()
Connection_List.append(clientsocket)
continue
else:
try:
data = i.recv(512)
except IOError:
data = ""
if data:
clientsocket = # <--- UPDATE CLIENT WITH CORRECT SOCKET
Request_Type = str(data.decode())
As you can see, what I need to do is make sure that when I receive data, the clientsocket that sent the data becomes the variable clientsocket, and not the last one that connected.
data = i.recv(512) but is there any way to determine what socket sent this?
Tried googling, but I don't think anyone has this problem. I hope that this isn't TOO specific. I'm sure someone else may want to determine the socket that sent the data too.
Upvotes: 1
Views: 1777
Reputation: 182619
Using the globally visible clientsocket
over and over is an accident waiting to happen. You should just change your code to use i
- that's a socket too.
data = i.recv(512) but is there any way to determine what socket sent this
Like I said, i
is that socket.
Upvotes: 1