title: UDP Socket Programming in C: 2 Clients and 1 Server
body: <p>Im very new to network programming.  I have a UDP client/server that sends a message to a server in lower or uppercase. The server receives the message and relays it back with switches cases.  I cant figure out how I would instead of relaying it back to the first client, sending it to client2.  heres my code.</p>
  
  <p>Server:</p>
  
  <pre><code>/*
  Simple udp server
  
  */
  #include&lt;stdio.h&gt;   //printf
  #include&lt;string.h&gt; //memset
  #include&lt;stdlib.h&gt; //exit(0);
  #include&lt;arpa/inet.h&gt;
  #include&lt;sys/socket.h&gt;
  #include&lt;ctype.h&gt;
  
  #define BUFLEN 512  //Max length of buffer
  #define PORT 8888   //The port on which to listen for incoming data
  
  void die(char *s)
  {
      perror(s);
  exit(1);
  }
  
  int main(void)
  {
  struct sockaddr_in si_me, si_other, si_other2;
  
  int s, i, slen = sizeof(si_other) , recv_len;
  char buf[BUFLEN];
  
  //create a UDP socket
  if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
  {
      die("socket");
  }
  
  // zero out the structure
  memset((char *) &amp;si_me, 0, sizeof(si_me));
  
  si_me.sin_family = AF_INET;
  si_me.sin_port = htons(PORT);
  si_me.sin_addr.s_addr = htonl(INADDR_ANY);
  
  //bind socket to port
  if( bind(s , (struct sockaddr*)&amp;si_me, sizeof(si_me) ) == -1)
  {
      die("bind");
  }
  
  //keep listening for data
  while(1)
  {
      printf("Waiting for data...");
      fflush(stdout);
  
      //try to receive some data, this is a blocking call
      if ((recv_len = recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *) &amp;si_other, &amp;slen)) == -1)   // read datagram from server socket
      {
          die("recvfrom()");
      }
  
      //print details of the client/peer and the data received
      printf("Received packet from %s:%d\n", inet_ntoa(si_other.sin_addr), ntohs(si_other.sin_port));         printf("Data: %s\n" , buf);
  
      //now reply to server socket/the client with the same data
      if (sendto(s, buf, recv_len, 0, (struct sockaddr*) &amp;si_other, slen) == -1)
      {
          die("sendto()");
      }
  
  
  
  }
  
  close(s);
  return 0;
  }
  </code></pre>
  
  <p>The Client:</p>
  
  <pre><code>/*
  Simple udp client
  
  */
  #include&lt;stdio.h&gt;   //printf
  #include&lt;string.h&gt; //memset
  #include&lt;stdlib.h&gt; //exit(0);
  #include&lt;arpa/inet.h&gt;
  #include&lt;sys/socket.h&gt;
  #include&lt;ctype.h&gt;
  
  #define SERVER "192.x.x.x"
  #define BUFLEN 512  //Max length of buffer
  #define PORT 8888   //The port on which to send data
  
  void die(char *s)
  {
  perror(s);
  exit(1);
  }
  
  int main(void)
  {
  struct sockaddr_in si_other;
  int s, s2, i, slen=sizeof(si_other);
  char buf[BUFLEN];
  char message[BUFLEN];
  
  if ( (s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)        // create a client socket
  {
      die("socket");
  }
  
  memset((char *) &amp;si_other, 0, sizeof(si_other));
  si_other.sin_family = AF_INET;
  si_other.sin_port = htons(PORT);
  
  if (inet_aton(SERVER , &amp;si_other.sin_addr) == 0)            // Create datagram with server IP and port.
  {
      fprintf(stderr, "inet_aton() failed\n");
      exit(1);
  }
  
  while(1)
  {
      printf("Enter message : ");
      gets(message);
  
  
      int a;
      char message2[BUFLEN];
      for(a=0;a&lt;=BUFLEN-1;a++)
        {
          if(message[a] &gt;= 97 &amp;&amp; message[a] &lt;= 122)
             message2[a] = toupper(message[a]);
          else
             message2[a] = tolower(message[a]);
  
        }
  
  
      if (sendto(s, message2, strlen(message2) , 0 , (struct sockaddr *) &amp;si_other, slen)==-1)
      {
          die("sendto()");
      }
  
  
      //receive a reply and print it
      //clear the buffer by filling null, it might have previously received data
      memset(buf,'\0', BUFLEN);
      //try to receive some data, this is a blocking call
      if (recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *) &amp;si_other, &amp;slen) == -1)        // read datagram from client socket
      {
          die("recvfrom()");
      }
  
      puts(buf);
  }
  
  close(s);
  return 0;
  }
  </code></pre>