SERVER HANDLING MANY CLIENTS USING FORK


AIM:
           To write a program for Server handling many clients using fork().

ALGORITHM:
SERVER:

    STEP 1: Start
    STEP 2: Declare the variables for the socket
    STEP 3: Specify the family, protocol, IP address and port number
    STEP 4: Create a socket using socket() function
    STEP 5: Bind the IP address and Port number
    STEP 6: Listen and accept the client’s request for the connection
    STEP 7: Create the child process for the server
    STEP 8: Display the child process ID and client’s message
    STEP 9: Close the socket
    STEP 12: Stop
CLIENT:
    STEP 1: Start
    STEP 2: Declare the variables for the socket
    STEP 3:  Specify the family, protocol, IP address and port number
    STEP 4: Create a socket using socket() function
    STEP 5: Call the connect() function
    STEP 6: Send the message to the server
    STEP 7: Close the socket
    STEP 8: Stop

SOURCE CODE:
SERVER:

#include<stdio.h>
#include<arpa/inet.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<netdb.h>
#define SERV_TCP_PORT 5035
int main(int argc,char**argv)
{
       int i,sockfd,newsockfd,clength;
       struct sockaddr_in serv_addr,cli_addr;
       char buffer[4098];
       pid_t childpid;
       sockfd=socket(AF_INET,SOCK_STREAM,0);
       serv_addr.sin_family=AF_INET;
       serv_addr.sin_addr.s_addr=INADDR_ANY;
       serv_addr.sin_port=htons(SERV_TCP_PORT);
       printf("\nStart");
       bind(sockfd,(struct sockaddr*)&serv_addr,sizeof(serv_addr));
       printf("\nListening...\n");
       listen(sockfd,5);
       i=1;
       for(;;)
       {
            clength=sizeof(cli_addr);
           listen(sockfd,5);
           newsockfd=accept(sockfd,(struct sockaddr*)&cli_addr,&clength);
           printf("\n\nAccepted");
           if((childpid=fork())==0)
           {
                printf("\nChild PID is : %d",getpid());
                read(newsockfd,buffer,4096);
                printf("\nClient Message : %s",buffer);
           }
           close(newsockfd);
           i++;
       }
       return 0;
}

CLIENT:
#include<stdio.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<netdb.h>
#define SERV_TCP_PORT 5035
int main(int argc,char*argv[])
{
       int sockfd;
       struct sockaddr_in serv_addr;
       struct hostent*server;
       char buffer[4096];
       while(buffer!="Quit")
       {
           sockfd=socket(AF_INET,SOCK_STREAM,0);
           serv_addr.sin_family=AF_INET;
           serv_addr.sin_addr.s_addr=inet_addr("127.0.01");
           serv_addr.sin_port=htons(SERV_TCP_PORT);
           printf("\nReady for sending\n");
           connect(sockfd,(struct sockaddr*)&serv_addr,sizeof(serv_addr));
           fgets(buffer,4098,stdin);
           write(sockfd,buffer,4098);
       }
       close(sockfd);
       return 0;
}

OUTPUT:
SERVER:



















CLIENT:


RESULT:
          Thus the program for server handling many clients was executed and the output was verified.
Previous
Next Post »

Still not found what you are looking for? Try again here.