Thursday, 26 July 2012

SIMPLE TCP ECHO SERVER PROGRAM USING LINUX SOCKET


/*SERVER PROGRAM OF ECHO SERVER*/
/*save this code
  as file name:echo_server.c
COMPILE:-
cc echo_server.c -o echo_server.out
RUN:-
./echo_server.out
 */
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<sys/un.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<signal.h>
#define PORT_NO 2048
main(int argc,char *argv[])
{
        int cli_sd,accept_cli_sd;
        int buf_len,cli_info_len;
        char buffer[256]={0};
        int port=0;
        struct sockaddr_in server_info,client_info;
        /* CREATION OF A SOCKET FOR ACCEPT CONNECTION FROM A CLINT*/
        accept_cli_sd=socket(PF_INET,SOCK_STREAM,0);
        if(accept_cli_sd==-1)
        {
                perror("socket():");
                exit(EXIT_FAILURE);
        }
        /*PREPARE SERVER INFORMATION TO sockaddr_in STRUCTURE*/
        /////////////as garbage can make  problems//////////
        //memset(&server_info,0,sizeof(struct sockaddr_un));
        server_info.sin_family=PF_INET;
        printf("argc=%d\n",argc);
        if(argc==2)
        {
                port=atoi(argv[1]);
                server_info.sin_port=htons(port);
        }
        else
        {
                server_info.sin_port=htons(PORT_NO);
        }
        server_info.sin_addr.s_addr=inet_addr("0.0.0.0");
        /* PREPARE SERVER TO ACCEPT CLINT BY ADDING SERVER PROPERTY
           TO ACCEPT_CLIENT SOCKET DESCRIPTOR*/
        if(bind(accept_cli_sd,(struct sockaddr *)&server_info,sizeof(struct sockaddr_un)))
        {
                perror("bind:");
                exit(EXIT_FAILURE);
        }
        /*DEFINIG THE QUEUE SIZE OF NEW CLINT REQUEST ON ACCEPT_CLINT SOCKET DESCRIPTOR*/
        listen(accept_cli_sd,3);
        /*NOW SERVER READY TO ACCEPT CLINT IN ACCEPT_CLLIENT SOCKET DESCRIPTOR*/
        /*SERVER USE ACCEPT() FUNCTION FOR ACCEPT CLINT REQUEST*/
        cli_info_len=sizeof(client_info);
        cli_sd=accept(accept_cli_sd,(struct sockaddr *)&client_info,&cli_info_len);
        if(cli_sd==-1)
        {
                perror("accept:");
                exit(EXIT_FAILURE);
        }
        printf("server listen to a clint::%s\n",(char*)inet_ntoa(client_info.sin_addr.s_addr));
        /*SERVER NOW READY TO SERVE A CLINT USING CLIENT_SOCKET DESCRIPTOR*/
        while(1)
        {
                bzero(buffer,sizeof(buffer));
                buf_len=read(cli_sd,buffer,255);
                buffer[buf_len]='\0';
                if(!(strcmp(buffer,"EXIT"))||(buf_len==0))
                {
                        break;
                }
                printf("%s\n",buffer);
                buf_len=write(cli_sd,buffer,255);
        }
        /*CLOSING OF CLIENT SOCKET DESCRIPTOR*/
        if(shutdown(cli_sd,SHUT_RDWR))
        {
                perror("shutdown:");
                exit(EXIT_FAILURE);
        }
        /*CLOSING OF ACCEPT_CLIENT SOCKET DESCRIPTOR*/
        if(shutdown(accept_cli_sd,SHUT_RDWR))
        {
                perror("shutdown:");
                exit(EXIT_FAILURE);
        }
        printf("socket descriptor properly closed\n");
        printf("server::Exiting\n");
}

No comments:

Post a Comment