The Server.

In this small series I'll show you how to write a simple UDP Client/Server system in a Unix environment using the C language. The server waits data listening on a port specified by the user, if the received data contains ".quit." string the server exits.

/*+--------------------------------------------------------------+
  |            server.c  -  description                          |
  |                -------------------                           |
  | begin      : 30/12/2009 18.31                                |
  | copyleft   : (C) 2009 xAppSoftware                           |
  | author     : Luigi D'Andrea                                  |
  | email      : gg1 ( at ) xappsoftware dot com                 |
  | compiling  : gcc -o server server.c                          |
  |                                                              |
  | Latest version on http://www.xappsoftware.com                |
  +--------------------------------------------------------------+
  | udp client-server may be redistributed and modified under    |
  | certain conditions. This software is distributed on an       |
  | "AS IS" basis WITHOUT WARRANTY OF ANY KIND, either express or|
  ! implied.  See the file License.txt for details.              |
  +--------------------------------------------------------------+*/

Blablablabla……

 

/*+--------------------------------------------------------------+
  | SYSTEM INCLUDES                                              |
  +--------------------------------------------------------------+*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>


/*+--------------------------------------------------------------+
  | SPECIFIC INCLUDES                                            |
  +--------------------------------------------------------------+*/
#include "clientserver.h"

Finally the main function, 

 

/*+--------------------------------------------------------------+
  | Function name :  main                                        |
  | Parameters    :  The port number                             |
  | Description   :  The simple udp server main                  |
  +--------------------------------------------------------------+*/
int main(int argc, char **argv)
{
	struct sockaddr_in   si_local, si_remote;
	int                  s;
	int                  port;
	unsigned int         slen;
	char                 buf[BUFLEN];

First of all I check for the command line argument. the server shall be launched with only one parameter, this parameter will set the port to listen to. the port number shall be > 1023 because Linux reserves port from 1 to 1023. 

	slen     =   sizeof(si_remote);
	if(argc!=2)
	{
		fprintf(stderr, "Usage: %s <port number>\n", argv[0]);
		exit(EXIT_FAILURE);
	}

	port=atoi(argv[1]);
	if(port<1024)
	{
		fprintf(stderr, "Usage: %s <port number>\n", argv[0]);
		fprintf(stderr, "\twhere <port number> shall be > 1023\n");
		exit(EXIT_FAILURE);
	}

Now I create a new UDP socket:

	if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1)
	{
		perror("socket");
		exit(EXIT_FAILURE);
	}

I bind the socket with the port.

	memset((char *) &si_local, 0, sizeof(si_local));
	si_local.sin_family       =  AF_INET;
	si_local.sin_port         =  htons(port);
	si_local.sin_addr.s_addr  =  htonl(INADDR_ANY);
	if (bind(s, (const struct sockaddr *)&si_local, sizeof(si_local))==-1)
	{
		perror("bind");
		exit(EXIT_FAILURE);
	}


infinite loop waiting for data. this loop will end when the server will receive ".quit."

	while(1)
	{
		memset(buf, 0, sizeof(char)*BUFLEN);

waiting for data

		if (recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *)&si_remote, &slen)==-1)
		{
			perror("recvfrom()");
			exit(EXIT_FAILURE);
		}

does data contains the quit command?

		if(strstr(buf, ".quit.")!=NULL)
		{
			printf("\".quit.\" Received \n");
			printf("Exiting\n");
			break;
		}
		else

No. data doesn't contain the quit command

		{
			printf("Received packet from %s:%d\n", inet_ntoa(si_remote.sin_addr),
                                ntohs(si_remote.sin_port));
			printf("Data: %s\n", buf);
		}
	}

release the socket.

	close(s);
	return 0;
}

See you later for the Client side,

gg1.

 

 

 

SIMPLE UDP CLIENT/SERVER

Network programming index:

The Server

The Client

Demonizing the Server

The source code