Handling Clients
As any employee ever will tell you, handling clients is the worst. They make it your problem to fix their problems. Fortunately, thanks to Rohan's code, the server won't have to deal with this. It's up to the clients to fix their own problems. Phew!
The Code
def handleClient(client, serverChannel, cID, clientele):
client.setblocking(1)
msg = ""
while True:
try:
msg += client.recv(10).decode("UTF-8")
command = msg.split("\n")
while (len(command) > 1):
readyMsg = command[0]
msg = "\n".join(command[1:])
serverChannel.put(str(cID) + " " + readyMsg)
command = msg.split("\n")
except:
clientele.pop(cID)
return
On a high level, you can think of this as a personal mail receptionist (could be any gender, really). So if Abhi (a client) wants to send some spicy memes to his relatives, he brings the memes (printed out, of course) to his personal receptionist (handleClient
) at the post office, who then puts them in the "to deliver" box (serverChannel
) for mail trucks to distribute later.
Scary while True
Loop #2
Receiving Messages
msg += client.recv(10).decode("UTF-8")
command = msg.split("\n")
The receptionist receives the message, decodes it, and splits it on "\n"
(a message delineator). Note that this happens all the time because it is in a while True
loop. Referencing our example, Abhi gives the receptionist all of his memes he wants to send, and the receptionist takes pile of memes and processes one meme at a time. Of course, Abhi's personal mail receptionist needs to be on 24/7 duty to be there for whatever meme Abhi wants to send.
Why "\n"
?
Cuz our Rohan chose it. For real. It can technically be any character (make sure to be consistent), it just needs to be there so we can know when a message is complete. Sockets sends messages in chunks, so this is a way to know when your message has fully arrived. Obviously, it would be foolish to choose something common like "a"
.
This is absolutely essential to include in all your messages, so make sure this delineator (whatever you choose) is consistent across your server and client files. Otherwise, you'll get weird bugs. Or an overload of memes. Unclear which is worse.
Not-So-Scary Nested while
Loop
while (len(command) > 1):
readyMsg = command[0]
msg = "\n".join(command[1:])
serverChannel.put(str(cID) + " " + readyMsg)
command = msg.split("\n")
Just some boring string formatting here. Not much to see. Except...
Putting the message on the server channel
serverChannel.put(str(cID) + " " + readyMsg)
This line puts your message on the server channel, and lets it be known that it was you who sent it. Later, this will be picked up by the serverThread and distributed. From our example, this is when our handy receptionist puts Abhi's memes into the "to deliver" bin.
Why is it in a loop?
Think about what would happen if Abhi wanted to send multiple different memes at one time to his friends. Well, it would be much more efficient for Abhi to give his receptionist a stack of all his memes on one trip to the post office rather than go home and back for each meme he wants to send.