Devember: Recursive echo Server

This post is part of the Devember collection, useful for the study of the Socket API.

Devember: Recursive echo Server

Today I’m going to introduce two useful functions for our network programs, exso_readln() and exso_writen(), clearly deriving from these and used in a course I’m attending at university.

The exso_readln function reads a line (a string ending with \n) from a socket buffer and automatically adds \0 at the end. The exso_writen function writes n bytes into a socket buffer. See here for more details: libexso.c.

The daytime server presents an iterative behaviour, therefore it can serve only one client at a time. To serve more than one client at a time, I should make a recursive server which spawns a child process to manage every incoming connection. This is achieved by calling fork():

pit_t childpid;
if ((childpid = fork()) == 0) {
	close(listenfd);
	server_echo(connfd);
	return 0;
}

The child process must close the listening socket before executing its tasks. Meanwhile, the father closes the connected socket and returns waiting for new requests. Server_echo is just a function I’ve made which executes all tasks of the server child process. You can also observe the new read and write functions:

void server_echo(int sockfd) {
	ssize_t n;
	char line[MAXLINE];
	while (1) {
		if ((n = exso_readln(sockfd, line, MAXLINE)) == 0) {
			return; // connection closed by the other end
		}
		exso_writen(sockfd, line, n);
	}
}

The complete code can be found on this repository.

5 December 2015