martes, 28 de febrero de 2012

Client-Server Mergesort

I always wanted to try some client-server stuff. Until now I've never programmed anything at all using client-server and sockets, and this week I decided to try something, seeing that I couldn't make any good progress with MPI for python, so that is what I'm going to explain, the results of my client-server script.

First of all, I started investigating about Python sockets, because I find Python to be a very comfortable language to program on(not simple, just comfortable). So searching in the internet I found a very simple example using sockets, but I couldn't understand it so I investigated more about each function that the program used. When I understood the majority of it, I started to modify it to create a mergesort. The result is the following:

Client Part:


The client side is pretty simple, it creates a socket to connect to the server, and asks for the list to be ordered. This input however, must be on a format X-X-X-X-X(where the X's are the numbers to order). The input then is sent to the server, and the server itself "translate" the string into a list of integers, so it is easier to order them. If the string is "exit", the client breaks the loop and closes the socket, if not then it will wait for the sorted list from the server.

If an incorrect input was sent to the server, the client prints just that "Invalid Input. Try Again". And finally if the input was correct, the client prints the sorted list


Server Part:



The server part is the one that will make the whole sorting operations and stuff. Here i have functions to do the mergesort, and another to convert the received string from the client to a manipulable list of integers. The server creates a socket, and binds it to a certain specified IP and port, in this case the IP is "localhost" to test it locally. Then we tell the socket to listen to a maximum number of n_sockets, specified in the execution arguments.

The while loop will append a number 1 to a list called clients, which will be used to determine the number of clients in the server(but in this case the program is pretty much limited to one, because with more, the server will only process them in the connection order), and then when the client disconnects using "exit", the server will pop a value from the list and check if it is empty, to close the server.

Execution

To execute the program, first the server code must be executed, using the syntax ./server.py port, where port is the number of the port to be used. If the port is not specified the program will use the port 9090 by default.
After the server, the client must be executed with the command: ./client.py nickname port, where the nickname is just a name to be used in the "command line", and the port is the port to connect, which must be the same than the server.

An example is the following:

Server Side:


Client Side:



Problems

The problem I still have with this code is that the server only works well with one single client. With more than one, the server has to wait to the input of the clients in the order they connect, which is horribly innefficient, but I'll try to fix that in the meantime.

1 comentario: