Welcome to my blog’s first tutorial series!
Ayyyyoooo guys! What’s good?
Continuing from my last post, its time to implement the server thread.
A lot of pieces are already in place to handle the server threads, so we should be able to get it done without too much hassle.
There are some minor code changes in the server script which I will only be covering in the video above, so if you’re only following the blog make sure to download the code at the end of the post and compare the changes.
Let’s write some codes!
Server Handler Code
# Server handler # -------------------------------------------- # This function will listen for connections as daemons # and keep connected in background. def ServerHandler(serverIP, serverPort, clientIP): sh = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sh.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) sh.bind((serverIP, serverPort)) sh.listen(1) # Listen for 1 client, each server = 1 client z,b = sh.accept() if (z): print "\n[" + GetTime() + "] Connection from " + str(b) while True: msg = Receive(z) if len(str(msg)): print "\n[" + GetTime() + "] " + msg
There isn’t much to the server handler. Basically we fire up a socket using the supplied arguments serverIP and serverPort – this port is the one supplied by the AllocAddress() function we created in the previous part (will basically add +1 to the same port of the main server).