/**
 * Group : Sangtae Park
 *         Sehoon Jung
 *         Edison Chindrawaly
 * Project: #1
 */
#include "header.h"
int main(int argc, char **argv)
{
   int            listenfd,        
                connfd,
            serverfd,
            clisize;         

   struct sockaddr_in   cliaddr,   
                  servaddr;

   struct sockaddr_in crypto_addr;  // Address of Crypto Server

   char recvline [MAXSIZE+1];          
   int n;                              
   MESSAGE recv_msg, send_msg;

   /* create socket */
   listenfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
   serverfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
 
   /* specify any connection is welcome */
   bzero(&servaddr, sizeof(servaddr));
   servaddr.sin_family = AF_INET;
   servaddr.sin_port = htons(9999);
   servaddr.sin_addr.s_addr = htonl(INADDR_ANY);

   /* specify address of Crypto Server */
   bzero(&crypto_addr, sizeof(crypto_addr));
   crypto_addr.sin_family = AF_INET;
   crypto_addr.sin_port = htons(SERV_PORT);
   crypto_addr.sin_addr.s_addr = inet_addr(argv[1]);

   /* Connect to Crypto Server */
   connect(serverfd, (struct sockaddr *)&crypto_addr, sizeof(crypto_addr));

   /* bind socket to port */
   bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr));

   /* listen on socket */
   listen(listenfd, 5);

   /* accept connection */
   clisize = sizeof(cliaddr);
   connfd=accept(listenfd, (struct sockaddr *)&cliaddr, (socklen_t *)&clisize); 
   cout << "Connection from: " << inet_ntoa(cliaddr.sin_addr) << endl;  
   char buffer[MAXSIZE+1];

   for ( ; ; ) {
      // Recieved encrypted message from Pete
      recv(connfd,(char*)(&recv_msg),sizeof(MESSAGE),0);
      printf("From pete: %s\n",recv_msg.msg);
      strcpy(buffer, recv_msg.msg);       

  
      // Send encrpyted message to Server to decrypt
      recv_msg.option=2;
      recv_msg.key='X';

      send(serverfd,(char*)(&recv_msg),sizeof(MESSAGE),0);  
      recv(serverfd,(char*)(&recv_msg),sizeof(MESSAGE),0);
      printf("From Server: %s\n",recv_msg.msg);
      if (strcasecmp(recv_msg.msg,"END")==0)
      {
        close(connfd);
        close(serverfd);
      exit(0);
      }

      // Mary Input
      printf("Mary: ");
      gets(buffer);    
      strcpy(send_msg.msg, buffer);

      // Send input to Crypto server to encrypt
      send_msg.option=1;
      send(serverfd,(char*)(&send_msg),sizeof(MESSAGE),0);

      // Receive encrpyted message from server 
      recv(serverfd,(char*)(&recv_msg),sizeof(MESSAGE),0);
      printf("From Server: %s\n",recv_msg.msg);

      // Send encrpted message to Pete
      send(connfd,(char*)(&recv_msg),sizeof(MESSAGE),0);     
      if(strcasecmp(buffer,"END") == 0)
      {
        close(serverfd);
        break;
      }

   }
   close(connfd);
}

 

1