A simple echo server

D33wakar 0 Tallied Votes 378 Views Share

This is a simple echo server that I've written while learning sockets programming.
Compile the server and run it. You can connect to it using telnet like this:

telnet localhost 1337

Disconnect the client by typing "/quit" without quotes.

/* A Simple server in C */
    
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>        /* misc. UNIX functions */
    
    #include <sys/socket.h>    /* socket definitions */
    #include <sys/types.h>     /* socket types */
    #include <arpa/inet.h>     /* inet (3) functions */
    
    int main(int argc, char* argv[]) {
    
        int list_s;    /* listening socket */
        int conn_s;    /* connecting socket */
        short int port = 1337; /*port number*/
        char* msg = "Welcome to the simple server v 0.1\n";  /* Welcome message */
        char buffer[1024] = {0};
    
        /* set up socket address structure */
        struct sockaddr_in servaddr;
        memset(&servaddr, 0, sizeof(servaddr));    /* set everything to zero */
        servaddr.sin_family = AF_INET;
        servaddr.sin_addr.s_addr = htonl(INADDR_ANY); 
        servaddr.sin_port = htons(port);
    
        /* create the listening socket */
        if((list_s = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
                printf("could not create socket\n");
                exit(EXIT_FAILURE);
                }
    
        /* bind the listening socket to server address and listen */
        if(bind(list_s, (struct sockaddr*) &servaddr, sizeof(servaddr)) < 0) {
            printf("binding failed, exiting.\n");
            exit(EXIT_FAILURE);
        }
    
        if(listen(list_s, 0) < 0) {
            printf("error calling listen()\n");
            exit(EXIT_FAILURE);
        }
    
        /* Enter an infinite loop to respond to client requests */
        while(1) {
            /* listen for the incoming clients */
            if((conn_s = accept(list_s, NULL, NULL)) < 0) {
                printf("error calling accept()\n");
                exit(EXIT_FAILURE);
            }
            /* Send the welcome message to the client */
            send(conn_s, msg, strlen(msg), 0);
            
    /**** using goto ****
     innerloop:
            recv(conn_s, buffer, sizeof(buffer), 0);
            if(strncmp(buffer,"/quit\r\n", strlen(buffer)) == 0) {
                if(close(conn_s) < 0) {
                    printf("error calling close()\n");
                    exit(EXIT_FAILURE);
                }
            } else {
                send(conn_s, buffer, sizeof(buffer), 0);
                memset(buffer, 0, sizeof(buffer));
                goto innerloop;
            }
            
    ****/
    
                do {
                    recv(conn_s, buffer, sizeof(buffer), 0);
                } while(strncmp(buffer,"/quit\r\n", strlen(buffer)) != 0 &&
                        send(conn_s, buffer, sizeof(buffer), 0)&&
                        memset(buffer, 0, sizeof(buffer)));
                    
            if(close(conn_s) < 0) {
                printf("error calling close()\n");
                exit(EXIT_FAILURE);
            }
    
            
        }            
        close(list_s); /* close the listening socket */    
        return 0;
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.