Network Programming: Day-Time Client Server program (Concurrent)

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 concurrent fashion using fork().
Server program:

#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);
  if(fork()==0){
//if in CHILD process
    close(listenfd);
    printf("SERVER: Connection established with connfd=%d\n", connfd);
//prepare data to be sent to connected client
    ticks = time(NULL);
    snprintf(buff,sizeof(buff),"%.24s\r\n--ByMonishGupta\r\n",ctime(&ticks));
    write(connfd,buff,strlen(buff));//send data to client
    close(connfd);//close client connection
    exit(0);//end child process
  }
  close(connfd);//close client connection with original server process
 }
 close(listenfd);//close listening socket
}
Client program:
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
//#include <time.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);
}

if( (sockfd = socket(AF_INET,SOCK_STREAM,0)) < 0) {
printf("Socket Error!!!");
exit(0);
}else
printf("CLIENT:Client sockfd = %d\n",sockfd);
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]);

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!!!");
exit(0);
}
else {
while( (n=read(sockfd,recvline,1000))>0) {
recvline[n] = 0; /* null terminate */
if(fputs(recvline,stdout) == EOF) {
printf("fputs error!!!");
exit(0);
}
}
}
if(n<0) {
printf("read error!!!");
exit(0);
}
exit(0);
}
More to come: Echo server client.

Cheers and try hosting at Linode.

3 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Hey guys can anyone please tell me how to display the weekday(Tue) along with the local time.I want to display the time in this format:
    MM/DD/YYYY HH:MM:SS Weekday

    Please reply now.

    ReplyDelete
  3. Gracefully written information on this blog are going to support me for my coming assignments. Every point was very clear and taught me few new parameters. I would like to use this information in coming future.ราคา server

    ReplyDelete