Network Programming: Date-Time Client Server program (Iterative)

This is the first post in network programming program series.
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.
Server program:

//SERVER PROGRAM: P1daytimeserver.c
//gives current server time to clients
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include  <time.h>
#define BACKLOG 10

int main(int argc,char **argv)
{
 int listenfd,connfd;
 struct sockaddr_in servaddr;
 char buff[1000];
 time_t ticks;        

 //server admin provides the servers IP address and port number
 if(argc != 2) {
  printf("Error with arguments!!! Usage:%s <portno>\n", argv[0]);
  exit(0);
 }
 
 //STEP 1: socket function
 listenfd = socket(AF_INET,SOCK_STREAM,0);
 printf("SERVER: Listening socket opened with listenfd = %d\n",listenfd);
 
 //STEP 2: initialize address structure
 bzero(&servaddr, sizeof(servaddr));
 servaddr.sin_family = AF_INET;
 servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
 servaddr.sin_port = htons(atoi(argv[1])); 
 
 //STEP 3: bind (int) listenfd and (struct sockaddr_in) servaddr
 bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
        
 //STEP 4: start listening to client requests
 listen(listenfd,BACKLOG);
 
 for( ; ; ) {
 //accept client request
  connfd = accept(listenfd,(struct sockaddr *)NULL,NULL);
  printf("SERVER: Connection established with connfd=%d\n", connfd);

//prepare data to be sent to connected client
  ticks = time(NULL);//read current time on server
  snprintf(buff,sizeof(buff),"%.24s\r\n--ByMonishGupta\r\n",ctime(&ticks));
  
  //send data to client
  write(connfd,buff,strlen(buff));
  
  //end client connection
  close(connfd);
 }
 
 //end listening socket
 close(listenfd);
}
Client program:
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, char **argv){
 int sockfd,n=0;
 char recvline[1000+1];
 struct sockaddr_in servaddr;

//client provides the servers IP address and port number
 if(argc != 3) {
  printf("Error with arguments!!! Usage:%s <IPv4 address of server> <portno>\n", argv[0]);
  exit(0);
 }
 
 //STEP 1: create socket
 if( (sockfd = socket(AF_INET,SOCK_STREAM,0)) < 0) {
  printf("Socket Error!!!");
  exit(0);
 }else
 //printf("CLIENT:Client sockfd = %d\n",sockfd);
 
 //STEP 2: create server address structure
 bzero(&servaddr, sizeof(servaddr));
 servaddr.sin_family = AF_INET;
 servaddr.sin_port = htons(atoi(argv[2])); 
 servaddr.sin_addr.s_addr = inet_addr(argv[1]); 
 
 //connect to server
 int conn_status = connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
 if(conn_status<0) {
  printf("CLIENT:connection function returns %d\n",conn_status);
  printf("Connect Error!!!");
  close(sockfd);
  exit(0);
 } else {
 //connection successful
  //read from server and print to client's stdout
  while( (n=read(sockfd,recvline,1000))>0) { 
   recvline[n] = 0; /* null terminate */
   if(fputs(recvline,stdout) == EOF) {
    printf("fputs error!!!");
    close(sockfd);
    exit(0);
   }
  }
 }
 if(n<0) {
  printf("read error!!!");
  close(sockfd);
  exit(0); 
 }
 close(sockfd);
}
More to come: Same program but server program with concurrent design.
Cheers and try hosting at Linode.

3 comments:

  1. Hey it throws an error message.Is this the correct code?

    ReplyDelete
  2. Hey it throws an error message.Is this the correct code?

    ReplyDelete