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


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

//    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

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License