Network Programming: Echo Client Server program (Iterative)

Echo server client: The way a server program and client(program ) interacts is what their protocol (set of rules they have for interation) is!! This server client program is echo program. Once successfully connected, Client writes anything. Server reads that and send back the same text. This goes on till client disconnects or writes text beginning with "exit". This is echo protocol.

All programs are tested in SUSE Linux Enterprise Server 10 (i586) VERSION = 10 PATCHLEVEL = 2. For how to check the distribution of linux you are running, read this.

The server serves the client one after the other in iterative fashion.
Echo Server program:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h> 
#include <sys/socket.h>
#include <netinet/in.h>

void error(const char *msg)
{
    perror(msg);
    exit(1);
}

int main(int argc, char *argv[])
{
     int sockfd, newsockfd, portno;
     socklen_t clilen;
     char buffer[256];
     struct sockaddr_in serv_addr, cli_addr;
     int n;
     if (argc < 2) {
         fprintf(stderr,"ERROR, no port provided\n");
         exit(1);
     }
  
//STEP1 socket function
     sockfd = socket(AF_INET, SOCK_STREAM, 0);
     if (sockfd < 0) 
        error("ERROR opening socket");
  
//STEP2 address structure
     bzero((char *) &serv_addr, sizeof(serv_addr));
     portno = atoi(argv[1]);
     serv_addr.sin_family = AF_INET;
     serv_addr.sin_addr.s_addr = INADDR_ANY;
     serv_addr.sin_port = htons(portno);

//STEP3 bind function
     if (bind(sockfd, (struct sockaddr *) &serv_addr,
              sizeof(serv_addr)) < 0) 
              error("ERROR on binding"); 

//STEP4 listen function
    listen(sockfd,10);
 
//STEP5 accept and serve clients one by one in iterative fashion
 while(1){
  clilen = sizeof(cli_addr);
  newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
   if (newsockfd < 0) error("ERROR on accept");
   bzero(buffer,256);
   while(  (n=read(newsockfd,buffer,255)) > 0  ){
   if (n < 0) error("ERROR reading from socket");
   n = write(newsockfd,buffer,n);
   if (n < 0) error("ERROR writing to socket");
   bzero(buffer,256);
   }
   close(newsockfd);
 }
     close(sockfd);
     return 0; 
}


Echo Client program:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h> 

void error(const char *msg)
{
    perror(msg);
    exit(0);
}

int main(int argc, char *argv[])
{
    int sockfd, n;
    struct sockaddr_in serv_addr;
    struct hostent *server;

    char buffer[256];
    if (argc < 3) {
       fprintf(stderr,"usage %s hostname port\n", argv[0]);
       exit(0);
    }

//STEP1 socket function
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd < 0) 
        error("ERROR opening socket");
    server = gethostbyname(argv[1]);
    if (server == NULL) {
        fprintf(stderr,"ERROR, no such host\n");
        exit(0);
    }
 
//STEP2 prepare address structure
    bzero((char *) &serv_addr, sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;
    bcopy((char *)server->h_addr, 
         (char *)&serv_addr.sin_addr.s_addr,
         server->h_length);
    serv_addr.sin_port = htons(atoi(argv[2]));

//STEP3 connect function: request to server for connection
    if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0) 
        error("ERROR connecting");
  
//STEP4 read and write to server.
    while(1){
  bzero(buffer,256);
  fgets(buffer,255,stdin);//read from client stdin
  if((n=bcmp("exit",buffer,strlen(buffer))==0)){ close(sockfd); return 0; }    
  n = write(sockfd,buffer,strlen(buffer));//write to server
  if (n < 0) error("ERROR writing to socket");
  bzero(buffer,256);
  n = read(sockfd,buffer,255);//read from server and save it in array buffer
  if (n < 0) error("ERROR reading from socket");
  printf("%s\n",buffer);//write to stdout the buffer's contents
    }
    close(sockfd);
    return 0;
}
Cheers and try hosting at Linode.

No comments:

Post a Comment