
/* WPsClient.c */       

/*  Client for the server for remote versions of the 
    w and ps commands.

    User can check load on machine without logging in 
    (or even without being able to log in). 

    Usage:  wps remotehostname command (where "command" 
    is either w or ps).
*/


/* these are needed for socket calls */
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <unistd.h>


#define WPSPORT 2088  /* server port number */
#define BUFSIZE 1000


main(int argc, char **argv)

{  int SD,MsgSize;
   struct sockaddr_in Addr;
   // struct hostent *HostPtr,*gethostbyname();
   struct hostent *HostPtr;
   char Buf[BUFSIZE];

   /* open a socket */
   SD = socket(AF_INET,SOCK_STREAM,0);

   /* set up for an Internet connection to the host whose 
      name was specified by the user on the command line */
   Addr.sin_family = AF_INET;
   Addr.sin_port = WPSPORT;
   /* get IP address of host */
   HostPtr = gethostbyname(argv[1]);
   memcpy(&Addr.sin_addr.s_addr,
      HostPtr->h_addr_list[0],HostPtr->h_length);

   /* OK, now connect */
   if (connect(SD,(sockaddr *) &Addr,sizeof(Addr)) < 0) printf("error\n");

   /* send user command */
   write(SD,argv[2],strlen(argv[2]));

   /* display response */
   MsgSize = read(SD,Buf,BUFSIZE);
   write(1,Buf,MsgSize);
}
      

