Monthly Archives: November 2011

Multi client chat server in C

I implemented a multi client chat server in C using socket programming. In a multi client chat server, N clients are connected to a server and send messages. In this program, one of the clients send messages to the server and it will send back the messages to all other clients. I implemented it using TCP.
A simple Client-Server Interaction


In the server program, first is the establishment of connection to a port. we get the socket file descriptor ,

int socket(int domain, int type, int protocol);

‘setsockopt’ is used for losing the pesky “Address already in use” error message. Once we have a socket, we might have to associate that socket with a port on our local machine. This can be done by using the ‘bind’.

int bind(int sockfd, struct sockaddr *my_addr, int addrlen);

We want to wait for incoming connections, we use ‘listen’ in this situation.

int listen(int sockfd, int backlog);

sockfd is the usual socket file descriptor from the socket() system call. backlog is the number of connections allowed on the incoming queue .

In the case of a server, it wants to listen for incoming connections as well as keep reading from the connections it already have. select() gives the power to monitor several sockets at the same time. It’ll tell you which ones are ready for reading, which are ready for writing, and which sockets have raised exceptions.

int select(int numfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout);

If we want to see if we can read from standard input and some socket descriptor, sockfd, just add the file descriptors 0 and sockfd to the set readfds. The parameter numfds should be set to the values of the highest file descriptor plus one. When select() returns, readfds will be modified to reflect which of the file descriptors we

selected which is ready for reading. After ‘select’, it run through the existing connections looking for data to read. If we got one, new connections are handled and the new file descriptor is added to the master set by keeping track of the maximum file descriptor. If there is no need to handle new connection, handle data from a client. If there is any data in the recv_buf, it can be received by using recv().Or , the data is send to all other clients by using the function send().


In the client program, first is the establishment of connection to the server and running on the localhost. Connection is established by using connect(). Then select() is used for either reading or writing as in the server program. It sends message to the server from the keyboard input using stdin. If there is data in the recv_buf, it receives data using recv().

The code of this program is available in

URL Shortener in Python with Google App Engine

According to wikipedia: URL shortening is a technique on the World Wide Web in which a Uniform Resource Locator (URL) may be made substantially shorter in length and still direct to the required page. This is achieved by using an HTTP Redirect on a domain name that is short, which links to the web page that has a long URL. This is especially convenient for messaging technologies such as Twitter and, which severely limit the number of characters that may be used in a message. Normally, a URL shortening service will use the top-level domain of a country that allows foreign sites to use its extension, such as .ly or .to (Libya and Tonga), to redirect worldwide using a short alphanumeric sequence after the provider’s site address in order to point to the long URL. Another use of URL shortening is to disguise the underlying address.

I implemented it in python with Google App Engine. Google App Engine (often referred to as GAE or simply App Engine, and also used by the acronym GAE/J) is a platform as a service (PaaS) cloud computing platform for developing and hosting web applications in Google-managed data centers. It virtualizes applications across multiple servers.

For deploying this project with google appengine, I developed and uploaded Python applications for Google App Engine using the App Engine Python software development kit (SDK).

The Python SDK includes a web server application that simulates the App Engine environment, including a local version of the datastore, Google Accounts, and the ability to fetch URLs and send email directly from our computer using the App Engine APIs.

Python App Engine applications communicate with the web server using the CGI standard. When the server receives a request for our application, it runs the application with the request data in environment variables and on the standard input stream (for POST data). To respond, the application writes the response to the standard output stream, including HTTP headers and content.

App Engine includes a simple web application framework of its own, called webapp. The webapp framework is already installed in the App Engine environment and in the SDK, so you do not need to bundle it with your application code to use it.

A webapp application has three parts:

  • one or more RequestHandler classes that process requests and build responses
  • a WSGIApplication instance that routes incoming requests to handlers based on the URL
  • a main routine that runs the WSGIApplication using a CGI adaptor.

The default datastore for an application is now the High Replication datastore.In my program, this High replication datastore stores the short URL of the corresponding original URL and when we give the same original URL, it gives the same short URL which is same as before.

Unlike a traditional web hosting environment, Google App Engine does not serve files directly out of our application’s source directory unless configured to do so. For that, we use static files. In my url shortener , I used stylesheets, images etc.

For testing the application: If we are not using Google App Engine Launcher, start the web server with the following command, giving it the path to the root directory. Here ‘url’ is my root directory.:

google_appengine/ url/

The web server is now running, listening for requests on port 8080. You can test the application by visiting the following URL in your web browser:

I registerd this application in Google App Engine using my gmail account.

We can create and manage App Engine web applications from the App Engine Administration Console, at the following URL: and uploaded it by running the following command:

google_appengine update url/

For my url shortener application, the URL for my website is

My urlshortener website snapshot is given below:


The complete code of this program is available in my bitbucket account. Here is the link: 

Flood fill

According to wikipedia: Flood fill also called Seed fill, is an algorithm that determines the area connected to a given node in a multi-dimensional array. It is used in the “bucket” fill tool of paint programs to determine which parts of a bitmap to fill with color, and in games such as Go and Minesweeper for determining which pieces are cleared. When applied on an image to fill a particular bounded area with color, it is also known as boundary fill.

I implemented it in python using pygame. The algorithm used is the depth-first algorithm(recursive). In my flood fill program, for every pixel filled, the functions analyze neighbor pixels: 4 neighbors (except diagonal neighbors); this kind of connectivity is called 4-connectivity. The code of this section is shown below:


 The complete code is available in . The following images are the snapshots taken during flood filling.

The Blog

The latest news on and the WordPress community.