title: udp client server program c
body: <p>I am writing a simple client and server program in C. I am able to send date from client to server. But, I am not able to send acknowledge from server to client. </p>
  
  <pre><code>/*******************udpserver.c*****************/
  int main(int argc, char *argv[])
  {
      /* Variable and structure definitions. */
      int sd, rc;
      struct sockaddr_in serveraddr, clientaddr;
      clientaddrlen = sizeof(clientaddr);
      int serveraddrlen = sizeof(serveraddr);
      char buffer[100];
      char *bufptr = buffer;
      int buflen = sizeof(buffer);
  
      if((sd = socket(AF_INET, SOCK_DGRAM, 0)) &lt; 0) {
          perror("UDP server - socket() error");
          exit(-1);
      }
  
      printf("UDP server - socket() is OK\n");
  
      memset(&amp;serveraddr, 0x00, serveraddrlen);
      serveraddr.sin_family = AF_INET;
      serveraddr.sin_port = htons(0);
      serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);
      if((rc = bind(sd, (struct sockaddr *)&amp;serveraddr, serveraddrlen)) &lt; 0) {
          perror("UDP server - bind() error");
          close(sd);
          exit(-1);
      }
  
      int addr_len = sizeof(serveraddr);
      if (getsockname(sd, (struct sockaddr *) &amp;serveraddr, &amp;addr_len)&lt;0) {
          perror("Error getting socket name.\n");
          return -1;
      }
      printf("Using IP %s and port %d\n", inet_ntoa(serveraddr.sin_addr), ntohs(serveraddr.sin_port));
      printf("UDP server - Listening...\n");
  
      rc = recvfrom(sd, bufptr, buflen, 0, (struct sockaddr *)&amp;clientaddr, &amp;clientaddrlen);
      if(rc &lt; 0) {
          perror("UDP Server - recvfrom() error");
          close(sd);
          exit(-1);
      }
  
     printf("UDP Server received the following:\n \"%s\" message\n", bufptr);
  
     printf("UDP Server replying to the UDP client...\n");
     rc = sendto(sd, bufptr, buflen, 0, (struct sockaddr *)&amp;clientaddr, clientaddrlen);
     if(rc &lt; 0) {
         perror("UDP server - sendto() error");
         close(sd);
         exit(-1);
     }
     printf("UDP Server - sendto() is OK...\n");
  
     close(sd);
     exit(0);
  }
  </code></pre>
  
  <p>My UDPClient program:</p>
  
  <pre><code>/****************udpclient.c********************/
  int main(int argc, char *argv[])
  {
      /* Variable and structure definitions. */
      int sd, rc;
      struct sockaddr_in serveraddr, clientaddr;
      int serveraddrlen = sizeof(serveraddr);
      char server[255];
      char buffer[100];
      char *bufptr = buffer;
      int buflen = sizeof(buffer);
      struct hostent *hostp;
      memset(buffer, 0x00, sizeof(buffer));
      /* 36 characters + terminating NULL */
      memcpy(buffer, "Hello! A client request message lol!", 37);
  
      if((sd = socket(AF_INET, SOCK_DGRAM, 0)) &lt; 0) {
          perror("UDP Client - socket() error");
          exit(-1);
      }
      else
          printf("UDP Client - socket() is OK!\n");
  
      if(argc != 3) {
          /*Use default hostname or IP*/
          printf("UDP Client - Usage &lt;Server hostname or IP&gt;\n");
          exit(0);
      }
  
      memset(&amp;serveraddr, 0x00, sizeof(struct sockaddr_in));
      serveraddr.sin_family = AF_INET;
      serveraddr.sin_port = htons(atoi(argv[2]));
  
      hostp = gethostbyname(argv[1]);
      if(hostp == (struct hostent *)NULL) {
          printf("HOST NOT FOUND --&gt; ");
          printf("h_errno = %d\n", h_errno);
          exit(-1);
      }
      else {
          printf("UDP Client - gethostname() of the server is OK... \n");
          printf("Connected to UDP server\n");
      }
      memcpy(&amp;serveraddr.sin_addr, hostp-&gt;h_addr, sizeof(serveraddr.sin_addr));
  
      rc = sendto(sd, bufptr, buflen, 0, (struct sockaddr *)&amp;serveraddr, sizeof(serveraddr));
      if(rc &lt; 0) {
          perror("UDP Client - sendto() error");
          close(sd);
          exit(-1);
      }
      else
          printf("UDP Client - sendto() is OK!\n");
  
      printf("Waiting a reply from UDP server...\n");
  
      rc = recvfrom(sd, bufptr, buflen, 0, (struct sockaddr *)&amp;serveraddr, &amp;serveraddrlen);
  
      if(rc &lt; 0) {
          perror("UDP Client - recvfrom() error");
          close(sd);
          exit(-1);
      } else {
          printf("UDP client received the following: \"%s\" message\n", bufptr);
      }
  
      close(sd);
      exit(0);
  }
  </code></pre>
  
  <p>When running the two programs, I am getting the following output: </p>
  
  <p>UdpServer:</p>
  
  <pre><code>$ ./UdpServer
  UDP server - socket() is OK
  Using IP 0.0.0.0 and port 49932
  UDP server - Listening...
  UDP Server received the following:
  "Hello! A client request message lol!" message
  UDP Server replying to the UDP client...
  UDP Server - sendto() is OK...
  </code></pre>
  
  <p>UdpClient:</p>
  
  <pre><code>$ ./UdpClient MyPC 49932 
  UDP Client - socket() is OK!
  UDP Client - gethostname() of the server is OK...
  Connected to UDP server
  UDP Client - sendto() is OK!
  Waiting a reply from UDP server...
  </code></pre>
  
  <p>UdpClient program is stuck at this point. Could anyone please explain what the problem is?</p>