Remort Shell

The client program running on the client computer should connect to the server program running on the server computer. Then the client program should prompt for commands from the user, make them run on the server computer and print results back on the client computer's screen. This command-result sequence should run in a loop so that a number of commands can get run on the server. When the user enters the text "exit" on the client program the system should terminate.

Implementation in C

client.c

/*
compile Command : gcc -o client client.c
                  ./client
*/
#include  <sys/types.h>
#include  <sys/socket.h>
#include  <sys/un.h>
#include  <stdio.h>
#include  <netinet/in.h>
#include  <netdb.h>
#include <stdlib.h>

#define  PORT  1030    /* addr to connect */
#define  HOST  "localhost"
#define BUFSIZE 256
/*#define  HOST  "10.16.48.56"*/

main()
{
    char buffer[BUFSIZE];
    char    c;
    FILE  *fp;
    register    int    s,len;
    struct    sockaddr_in    sin,pin;
    struct    hostent    *hp;
    //Retreive the host information
    if  ((hp=gethostbyname(HOST)) == 0)  {
        perror("gethostbyname");
        exit(1);
    }

    bzero(&pin, sizeof(pin));//Writes 0's to a byte string
    pin.sin_family = AF_INET;
    pin.sin_addr.s_addr= ((struct  in_addr *) (hp->h_addr)) -> s_addr;
    pin.sin_port=htons(PORT);

    while(1){

    printf("Messege : ");
        bzero(buffer,BUFSIZE);//Set the buffer to zero
    fgets(buffer,BUFSIZE,stdin);//Get the user input

    //If buffer is exit, exit from the client program
    if(strstr(buffer,"exit") != NULL){
        printf("\nClient exit from the server successfully........\n");
        exit(0);
    }
    //Create a socket to communicate with server
    if ((s=socket(AF_INET,SOCK_STREAM,0)) < 0)   {
        perror("client:socket");
        exit(1);
    }
 
    //Connect to the server (Initiate a connection on a socket)
    if (connect(s,(struct sockaddr *) &pin,sizeof(pin)) < 0) {
        perror("client:connect");
        exit(1);
    }
 
    //Write buffer to the socket 
    int n = write(s,buffer,BUFSIZE);
    if (n < 0)
            error("ERROR writing to socket");
    bzero(buffer,BUFSIZE);//Set the buffer to zero

    fp = fdopen(s,"r");//Read the server responds from the socket
    //Client reads character by character until end of file
    while ((c=fgetc(fp)) != EOF)
        putchar(c);
    putchar('\n');
    }
    close(s);//Close the socket
    exit(0);
}


server.c

/*
Compile Command : gcc -o server server.c
                  ./server
*/
#include  <sys/types.h>
#include  <sys/socket.h>
#include  <sys/un.h>
#include  <stdio.h>
#include  <string.h>
#include  <fcntl.h>
#include  <unistd.h>
#include  <sys/param.h>
#include  <netinet/in.h>
#include  <netdb.h>
#include <stdlib.h>

#define  PORT  1030    /* addr to connect */
#define BUFSIZE 256

main()
{
    int    fromlen;
    register    int    s,ns,len;
    struct    sockaddr_in    sin,pin;
    char    hostname[MAXHOSTNAMELEN];
    int    p;int n;
        char buffer[BUFSIZE];
    //Create a socket for communication
    if  ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0)  {
        perror("server: socket");
        exit(1);
    }
    //Structure sin holds communication domain,IP address and port number
    sin.sin_family = AF_INET;
    sin.sin_addr.s_addr=INADDR_ANY;
    sin.sin_port=htons(PORT);//Convert the PORT number from host byte order to network byte order

    //Attaches particular port number to the socket
    if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0)  {
        perror("server:bind");
        exit(1);
    }
 
    while (1) {
     
        //Listen for connection on a socket
        if (listen(s,5) <0)  {
                perror("server: listen");
                exit(1);
            }
        //Accept a connection on a socket
        if ((ns = accept(s, (struct sockaddr *)&pin, &fromlen)) < 0)  {
            perror("server: accept");
            exit(1);
        } 
        //Create a new process to handle the client
        if  ((p=fork()) ==0)
        {
            close(s);
            bzero(buffer,BUFSIZE);//Set the buffer to zero
                n = read(ns,buffer,BUFSIZE);//Read the client request through the socket
              printf("Request : %s",buffer);
               if (n < 0) error("ERROR reading from socket");
            dup2(ns,1);//Server screen re-direct to client screen
            dup2(ns,2);//Server error file discripter redirect to client screen
            system(buffer);//Execute the client request through the shell
            send(ns,"\n",1,0);
            close(ns);
            exit(0);
        }
        else
            close(ns);
    }
    close(s);//Close the socket
    exit(0);
}

Comments

Popular posts from this blog

Solve the Maze by DFS (Depth First Search)

Rabin Karp Algorithm

Text-to-Speech converter