title: Echo Server Client using UDP Sockets in C
body: <p>The following program has the motive of sending a data gram from client to the server and the data gram being echoed back to the client as an acknowledgment. </p>
  
  <p>I have tried to terminate the program when an empty string is
   accepted from the server side.
   I executed the program in Fedora 14. 
   The error I encounter is that there is an "Error in SendTo"- Socket on
   Non-Socket.</p>
  
  <p>I am unable to figure out the mistake. Please do point out the mistake. Suggestions are welcome. </p>
  
  <p>I have tried reading other resources regarding this, but have not been able to get a hold out of it. </p>
  
  <p>Thanks in advance for your answer.</p>
  
  <p>Server:</p>
  
  <pre><code>#include&lt;stdio.h&gt; 
  #include&lt;sys/types.h&gt; 
  #include&lt;netinet/in.h&gt;
  #include&lt;sys/socket.h&gt; 
  #include&lt;string.h&gt; 
  #include&lt;unistd.h&gt; 
  #include&lt;stdlib.h&gt;
  #define PORT 7844
  
  int main(int argc,char *argv[]) {
      char buf[2000];
      int sockfd,len,a;
      struct sockaddr_in servaddr,cliaddr;
      if((sockfd=socket(AF_INET,SOCK_DGRAM,0))&lt;0)
      {
          printf("Error creating socket\n"); 
          exit(0);
      }
      printf("UDP Server Socket Created Successfully.\n"); 
      bzero(&amp;servaddr,sizeof(servaddr));
      servaddr.sin_family=AF_INET;
      servaddr.sin_port=htons(PORT); 
      servaddr.sin_addr.s_addr=htonl(INADDR_ANY); 
      if(bind(sockfd,(struct sockaddr *)&amp;servaddr,sizeof(servaddr))&lt;0)
      {
          perror("Error binding socket."); 
          exit(0);
      }
      printf("UDP Server Socket Binded.\n"); 
      len=sizeof(cliaddr);
      do {
          strcpy(buf," "); 
          if((a=recvfrom(sockfd,buf,sizeof(buf),0,(struct sockaddr *)&amp;cliaddr,&amp;len))&lt;0){
              perror("ERROR"); 
              exit(0);
          }
          printf("From Client : %s",buf); 
          if((sendto(sockfd,buf,a,0,(struct sockaddr *)&amp;cliaddr,len))&lt;0)
          {
              perror("NOTHING SENT"); 
              exit(0);
          }
          printf("Server : ");
          fgets(buf,sizeof(buf),stdin);
          if((sendto(sockfd,buf,a,0,(struct sockaddr *)&amp;cliaddr,len))&lt;0)
          {
              perror("NOTHING SENT"); 
              exit(0);
          }
      } while(strcmp(buf," ")!=0); 
      close(sockfd); 
      return 0;
  }
  </code></pre>
  
  <p>CLIENT</p>
  
  <pre><code>#include&lt;stdio.h&gt;
  #include&lt;sys/types.h&gt; 
  #include&lt;netinet/in.h&gt; 
  #include&lt;sys/socket.h&gt; 
  #include&lt;arpa/inet.h&gt;
  #include&lt;string.h&gt; 
  #include&lt;unistd.h&gt; 
  #include&lt;stdlib.h&gt;
  #define PORT 7844
  
  int main(int argc,char *argv[]) {
      int sockfd,len;
      struct sockaddr_in serv,cliaddr;
      char buff[2000]; 
      if((sockfd=socket(AF_INET,SOCK_DGRAM,0))&lt;0)
      {
          perror("ERROR creating socket");
          exit(0);
      }
      printf("UDP Client Socket Created Successfully.\n"); 
      memset(&amp;serv,0,sizeof(serv)); 
      serv.sin_family=AF_INET; 
      serv.sin_port=htons(PORT);
      serv.sin_addr.s_addr=inet_addr(argv[1]);
      do
      {
          printf("Client : ");
          fgets(buff,sizeof(buff),stdin);
          if((sendto(sockfd,buff,sizeof(buff),0,(struct sockaddr *)&amp;serv,sizeof(serv)))&lt;0) {
              perror("ERROR IN SENDTO");
          }
          if(strcmp(buff," ")==0) {
              exit(0);
          } 
          strcpy(buff," ");
          if((recvfrom(sockfd,buff,sizeof(buff),0,(struct sockaddr *)&amp;cliaddr,&amp;len))&lt;0) {
              perror("ERROR IN RECVFROM"); 
              exit(0);
          }
          fputs("From Server : ",stdout); 
          fputs(buff,stdout); 
  
      } while(strcmp(buff," ")!=0);
      close(sockfd);
      return 0;
  }
  </code></pre>
  
  <p>Please let me know if I can terminate a program using empty string. 
  Also let me know of flaws in the program.
  Thank you, once again.</p>