Programming: Issue #2 [Sockets][Time][Python]: Basic Sockets 2

Today I’m going to explain a socket program that is kind of more useful than first one, since a client that does not receive any data back from the server has very limited uses (it can still be used as a remote controller, but you can’t even tell if server has received correctly the commands/data sent to it).

For this one I’m going to use the Time library. The client will be able to make a request to the server for it to send back his local time. This will require the use of a bidirectional socket, as well as a function to calculate and format the time data.

Let’s begin with the server code:

I assume you’re reading this after having seen Issue #1, so the new part starts after the ‘res = add.recv(255)’ line. Here I’m using a basic control flow, the ‘if’ sentence, to check if the client has send a string that is exactly “time”. If this is the case, the return message will be the server’s local time that we will get from another python file. Otherwise, we will tell the client the command was incorrect.

Why use another python file for the time function?

This is basically to keep the server code clean and to be able to reuse that function code without needing to import this very own server file. Having another file for the functions will also allow us to keep many other funcions the server will handle separated from the basic iteration code. Note the ‘import’ at the begining of the file making reference to the file that contains the function, server_functions.py (the import sentence doesn’t need the .py extension).

After the command has been processed, comes another flow control sentence. This contains a call to the sleep method from the time library, which stops the process for the value provided as a parameter, in seconds. I do this just to make sure the client has time to reach the LISTENING state on it’s port so the connection doesn’t break. If we skip this, it is possible that the server sends the data before the client is ready to receive it, specially using loopback direction on the same machine,as we cannot tell which process will end faster. The 3 seconds of this example are too much for this, probably half a second delay would be far enough.

The last sentence before closing the socket sends the variable msg back to the client. Note we’re using the ‘add’ variable, which contains information about the connection from the client to send him a response.

The client file is pretty much the same as in Issue #1, it just adds another line to receive data -like as we did on the server to send it back- from the server and print its content before closing the socket:

And here’s the code for the time function:

We could just send back the ‘hour’ list, the ‘for’ iteration is just to make sure it always returns hour in HH:MM:SS format. If we don’t edit this way (or any other that equals this) it could return HH:M:SS or H:MM:S when the hour/minute/second values are less than 10.

After this tutorial, anyone who has researched a little bit about sockets or that is really understanding what is reading here and knows how they work could ask “And how does the client receive data back if we haven’t set a socket number nor ip adress for it?”. And that’s a good question. The server socket needs to know which port must be the one used for listening, but the client does not need to have a specific port number for itself to send the data, anyone works fine. So it basically takes a random port assignated by the operating system. If we want this not to be random, we can add a bind() sentence to the client port as we do with the server one after creating it.

This ends with this second issue of the programming blog.

Issue 2 files

This entry was posted in Programming and tagged , , , , , . Bookmark the permalink.

Leave a comment