title: socket programming getting input
body: <p>I am trying to establish a connection between client and server and when once it's successful,
  a menu screen will be shown prompting the user whether he wants to 
  1)input a message 
  2)exit the program.</p>
  
  <p>the problem I face was when I key in one, It will return me the output shown below</p>
  
  <p>Client
  output(It will just keep returning "I got your msg whenever I enter '1' without having the user to input the message first until I press ctrl+c")</p>
  
  <pre><code>Main Menu
          ----------------------
   1. option 1
   2. option 2
   Enter your choice1
   Please enter the message: I got your msg
   Main Menu
          ----------------------
   1. option 1
   2. option 2
   Enter your choice1
   Please enter the message: I got your msg
   Main Menu
          ----------------------
    1. option 1
    2. option 2
    Enter your choice1
    Please enter the message: I got your msg
  </code></pre>
  
  <p>Server</p>
  
  <pre><code>  Here is the msg:
  
    Here is the msg:
  
    Here is the msg:
  </code></pre>
  
  <p>What I have currently.</p>
  
  <p>server.c</p>
  
  <pre><code>#include &lt;sys/socket.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;arpa/inet.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;errno.h&gt;
  #include &lt;string.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;stdbool.h&gt;
  
  void doPrcoessing(int sock) {
  int n;
  char buffer[256];
  bzero(buffer,256);
  n =read(sock,buffer,255);
  
  if(n &lt; 0) {
      perror("Error from reading socket");
      exit(1);
  }
  
      printf("Here is the msg: %s\n",buffer);
      n=write(sock,"I got your msg",18);
  
  if(n&lt;0) {
      perror("Error writing to socket");
      exit(1);
  }
  }
  
  int main( int argc, char *argv[] )
  {
      int sockfd,pid,newsockfd,portno,clientn;
      struct sockaddr_in serv_addr,cli_addr;
      char buffer[256];
      bool connected;
      int n;
  
      /*call socket() function*/
      sockfd = socket(AF_INET, SOCK_STREAM, 0);
      if(sockfd &lt; 0)
      {
      perror("Error opening socket");
      exit(1);
      }
     // printf("socket retrieve success\n");
  
     /*Initialize socket structure*/
  
      bzero((char *) &amp;serv_addr, sizeof(serv_addr));
      portno = atoi(argv[1]);
      serv_addr.sin_family = AF_INET;
      serv_addr.sin_addr.s_addr = INADDR_ANY;
      serv_addr.sin_port = htons(portno);
  
      /*bind the host address using bind() call*/
      if(bind(sockfd,(struct sockaddr *) &amp;serv_addr,sizeof(serv_addr)) &lt; 0) {
      perror("Error on binding!");
          exit(1);
      }
      int yes =1;
      if(setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&amp;yes, sizeof(yes)) == -1) {
      perror("setsockopt");
      exit(1);
      }
      /*start listening for clients, will go into sleep mode and wait for incoming     connection*/
      listen(sockfd,5);
  clientn = sizeof(cli_addr);    
   while(connected == false) {
      newsockfd = accept(sockfd,(struct sockaddr*)&amp;cli_addr,&amp;clientn);
  
      if(newsockfd &lt; 0) {
          perror("Error on accept!");
          exit(1);
          }
  
      /*Create chlid process*/
      pid =fork();
      if(pid &lt; 0)
      {
          perror("Error on fork!");
          exit(1);
      }
      if(pid == 0)
      {
          /*Client process*/
          close(sockfd);
          doPrcoessing(newsockfd);
          return 0;   
      }
      else
      {
          close(newsockfd);
      }
  }
  return 0;
  </code></pre>
  
  <p>}</p>
  
  <p>client.c</p>
  
  <pre><code>#include &lt;sys/socket.h&gt; 
  #include &lt;netinet/in.h&gt;
  #include &lt;arpa/inet.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;errno.h&gt;
  #include &lt;string.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;netdb.h&gt;
  #include &lt;stdbool.h&gt;
  
  void print(void);
  
  int sockfd, portno, n; 
  struct sockaddr_in serv_addr;
  struct hostent *server;
  bool loop = false; 
  char options[100];
  char buffer[256];
  
  int main(int argc, char *argv[])
  {  
  while(loop==false) {
      if (argc &lt; 3) {
          fprintf(stderr,"usage %s hostname port\n", argv[0]);
          exit(0);
      }
      portno = atoi(argv[2]);
   /* Create a socket point */
      sockfd = socket(AF_INET, SOCK_STREAM, 0);
      if (sockfd &lt; 0)  {
          perror("ERROR opening socket");
          exit(1);
    }
   server = gethostbyname(argv[1]);
   if (server == NULL) {
          fprintf(stderr,"ERROR, no such host\n");
          exit(0);
   }
  
   bzero((char *)&amp;serv_addr, sizeof(serv_addr));
   serv_addr.sin_family = AF_INET;
   bcopy((char *)server-&gt;h_addr,(char *)&amp;serv_addr.sin_addr.s_addr,
              server-&gt;h_length);
      serv_addr.sin_port = htons(portno);
  
   /* connect to the server */
      if (connect(sockfd,(struct sockaddr *)&amp;serv_addr,sizeof(serv_addr)) &lt; 0) {
          perror("Error connecting");
          exit(1);
   }
  
  int choice;  
  //print();
      if(choice !=2) {
          printf("Main Menu");
          printf("\n\t----------------------");
          printf("\n 1. option 1");
          printf("\n 2. option 2");
          printf("\n Enter your choice");
          scanf("%d",&amp;choice);
          switch(choice) {
          case 1: print();
              break;
          case 2: exit(0);
              break;
          default: printf("Invalid choice!\n");
              break;
  
          }   
      }
  }
  close(sockfd);
      return 0;
  }
  
  void print(void)    {
   /*ask for a message from the user, this message will be read by server */
  printf("Please enter the message: ");
          bzero(buffer,256);
              fgets(buffer,255,stdin);
          /* Send message to the server */
              n = write(sockfd,buffer,strlen(buffer));
          if (n &lt; 0)  {
                  perror("ERROR writing to socket");
                  exit(1);
              }
  
          /* read server response */
              bzero(buffer,256);
              n = read(sockfd,buffer,255);
              if (n &lt; 0)  {
                  perror("ERROR reading from socket");
                  exit(1);
              }
  
          printf("%s\n",buffer);  
  }
  </code></pre>