/* NAME : rpipe.C TYPE : C++ SOURCE AUTHOR : Arunkumar Elango DESCRIPTION : This file contains the source code for the client. It takes the following command line as input : $ rpipe [-h hostname] [-p portnumber] executable_name The client tries to connect to the server at the known portnumber and host. If it succeds in connecting, it sends a password and the executable_name. The server checks the validity of the password, and if it is right, sends back an acknowledgement. Else, the child server exits and the connection is lost. Once the client receives the acknowledgement, it reads its standard input and sends the data to the server through the socket. The server processes the request and sends the output back through the socket. The client just prints the output it recieved onto the screen and exits. */ #include #include #include #include #include #include #include #include #include const int MAXSTR=1024; const long PORTNUM=54715; const int PASSWORD = 21; main( int argc, char *argv[]) { int portNumber; char executableName[MAXSTR],hostName[MAXSTR]; if ( (argc != 6) && (argc != 4) && (argc != 2) ) { cerr << "Invalid command line.\n"; exit(1); } if ( argc == 2 ) { strcpy(hostName,"ernie.eecs.uic.edu"); portNumber = PORTNUM; strcpy(executableName,argv[1]); } else if (argc == 4 ) { if (!strcmp(argv[1],"-h")) { strcpy(hostName,argv[2]); portNumber = PORTNUM; } else if (!strcmp(argv[1],"-p")) { portNumber = atoi(argv[2]); strcpy(hostName,"ernie.eecs.uic.edu"); } else { cerr << "Invalid command line.\n"; exit(1); } strcpy(executableName,argv[3]); } else { if ((!strcmp(argv[1],"-h")) && (!strcmp(argv[3],"-p"))) { strcpy(hostName,argv[2]); portNumber = atoi(argv[4]); } else { cerr << "Invalid command line.\n"; exit(1); } strcpy(executableName,argv[5]); } if (portNumber < 10000) { cerr << "Port number has to be atleast 10000.\n"; exit(1); } // cerr << hostName << " " << portNumber << executableName <<"\n"; struct hostent *hostptr; if ((hostptr = gethostbyname(hostName)) == NULL) { cerr <<"Error locating host : "<h_addr,hostptr->h_length); serv_addr.sin_port = htons(portNumber); int sockfd,newsockfd; if ((sockfd = socket(AF_INET,SOCK_STREAM,0)) < 0) { cerr << "client : socket error.\n"; exit(1); } if ( connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0) { cerr << "client : connect error.\n"; exit(1); } char buf[MAXSTR],ack[MAXSTR],data[MAXSTR]; char answer[MAXSTR]; int i=0; char ch='0'; sprintf(buf,"%d %s\n",PASSWORD,executableName); buf[strlen(buf)] = '\0'; write(sockfd,buf,strlen(buf)); read(sockfd,(void *)ack,MAXSTR); // cerr << "got back " << ack << "from server"; if (( ack[0] == 'N') && (ack[1] == 'O') ) { cerr << "Connect request refused.\n"; exit(1); } cin.getline(data,MAXSTR,'\0'); data[strlen(data)] = '\0'; // cerr << "Client : sending.." << data << endl; write(sockfd,data,strlen(data)); read(sockfd,(void *)answer,MAXSTR); cerr << answer <