RSS LinkedIn Twitter

Single Threaded Server in C++

July 28th, 2009 Categories: Uncategorized

The versatility of the client-server architecture has always fascinated me. You can write a server once, and plug in whatever protocol you happen to be implementing. The same goes for clients.

I’m going to present here a simple server I implemented in C++. I guess if I had to give a name to the protocol I implemented, I would call it the “Echo Protocol”. There’s only one part of the code I’m going to comment on specifically in this article, and that is the process() method.

There are ample sources on the web and otherwise explaining the use of the socket, bind, listen, and accept methods – which are the basis for socket communication in C and C++.

One book I would recommend for someone serious about network programming (though it hardly needs my recommendation) is UNIX Network Programming, Third Edition by W. Richard Stevens, Bill Fenner and Andrew Rudoff. If you do read this book, be aware that they use wrapper functions throughout the book (i.e. Bind(), Listen()) to add a measure of error checking (see section 1.4 for their explanation). The wrapper functions have the same signature as the corresponding BSD functions, just make them lowercase.

Here is the code for my process() method:

[cpp]
void process() {
// Read the request
//——————
string request = "";

while(! isRequestDone(request) ) {
// Read from the client
//—————————–
char buf;
bzero(&buf, 1);
nread = recv(client_fd, &buf, 1, 0);
request += buf;

if (nread < 0) {
if (errno == EINTR)
continue;// Continue reading
else
break; // No more
}
else if (nread == 0) {
break; // Nothing left to read
}
}

// Do something with the request
//——————————

// Send the response:
//——————————
string response = request;

int num_b = response.length();
for (int i = 0; i < num_b; i++) {
char cur = response[i];
send(client_fd, &cur, 1, 0);
}

// Close the connection here
close(client_fd);
}
[/cpp]

This method is where you would implement the protocol (be it HTTP, or what have you). A crucial step in any interesting protocol would be parsing out the request (something you might do where I have the comment “Do something with the request”) and using that information to construct the response. Sometimes the request parsing is handled while the request is being received. I wanted to keep this example simple (so you’ll also notice I’m receiving and sending only 1 byte at a time… which involves a lot of overhead and I wouldn’t recommend for high load servers sending and receiving larger messages). You could increase the number being read at a time by increasing the size of buf and/or cur.

You can download the complete code for this server here: C++ single threaded server

You can compile this code on Linux with the following command:

[bash]g++ -Wall -o bin/server single_threaded_server.cpp[/bash]

Linux and Windows both use BSD sockets, but I haven’t tried compiling this code on Windows without using g++ on cygwin. You’re on your own there.

This post is part of a series, next I’m going to post a simple C++ client. Then I’m going to present a threaded server architecture, and finally I’m going to post a Flex chat client and implement a simple chat protocol. Stay tuned.

Tags: , , , ,
No comments yet.

Leave a Comment

*