Echo Server In C
The code below will demonstrate how to write a simple echo server in C
First The following set of files need to be included
#include<stdio.h> #include<unistd.h> #include<stdlib.h> #include<string.h> #include<sys/socket.h> #include<arpa/inet.h>
we need the following data structures
int server_socket_descriptor; //Descriptor for Server Socket int client_socket_descriptor; //Descriptor for Client Socket struct sockaddr_in server_address; //Data Structure for Server Socket struct sockaddr_in client_address; //Data Structure for Client Socket unsigned short server_port;
Step 1: This will involve setting the parameters of the Server Side Socket
memset(&server_address,0,sizeof(server_address)); // This part sets the parameters of the server // INADDR_ANY basically means that assign any address server_address.sin_family = AF_INET; server_address.sin_addr.s_addr = htonl(INADDR_ANY); server_address.sin_port = htons (server_port);
the code is pretty self explanatory. The only thing that merits explanantion is the use of INADDR_ANY, which is basically a constant that tells the server to acquire any available IP address
page revision: 2, last edited: 27 Apr 2008 12:27