title: Simple UDP socket code, sending and receiving messages
body: <p>I am just learning UDP sockets and this is my first code involving it. I have two programs which send and receive messages back and forth. My question is it seems I have to declare which IP address I am sending/receiving from multiple times throughout the code as it changes but I feel there is a better way to do this without changing the inet_addr manually within the codes. From my reading it looks like sendto and recvfrom may be able to help but I am unsure how to use them in this context. If anyone could show me how to fix my simple problem I would greatly appreciate it! Thanks</p>
  
  <p>CODE 1: Send then Receive</p>
  
  <pre><code>int main(int argc, char *argv[])
  {
  //initialize socket and structure
  int socket_info;
  struct sockaddr_in server;
  char message[100];
  char incoming_message[100];
  
  printf("Input Message: ");
  fgets(message, 100, stdin);
  
      //create socket
      socket_info = socket(AF_INET, SOCK_DGRAM, 0);
      if (socket_info == -1) {
      printf("Could not create socket");
      }
  
  //assign local values
      server.sin_addr.s_addr = inet_addr("172.21.8.178");
      server.sin_family = AF_INET;
      server.sin_port = htons( 1100 );
  
      //binds connection
      if (bind(socket_info, (struct sockaddr *)&amp;server, sizeof(server)) &lt; 0) {    
  perror("Connection error");
         return 1;
      }
      puts("Bind");
  
      //assign new value to connect to
      server.sin_addr.s_addr = inet_addr("172.21.8.179");
  
      //checks connection 
      if (connect(socket_info, (struct sockaddr *)&amp;server, sizeof(server)) &lt;       0) {
      perror("Connection error");
         return 1;
      }
      puts("Connected");
  
      //sends message
  if(send(socket_info, message, strlen(message), 0) &lt;0) {        
  perror("Send failed");
      return 1;
      }
      puts("Message Sent");
  
  //receives message back    
  if(recv(socket_info, incoming_message, sizeof(incoming_message), 0) &lt;0) {    
  puts("Received failed");
      return 1;
      }
      puts("Message received");
      puts(incoming_message);
  
  close(socket_info);
  
  }
  </code></pre>
  
  <p>CODE 2: Receive then Send</p>
  
  <pre><code>int main(int argc, char *argv[])
  {
  //initialize socket and structure
  int socket_info;
  struct sockaddr_in server;
  char incoming_message[100];
  
      //create socket
      socket_info = socket(AF_INET, SOCK_DGRAM, 0);
      if (socket_info == -1) {
      printf("Could not create socket");
      }
  
      //assign values
      server.sin_addr.s_addr = inet_addr("172.21.8.179");
      server.sin_family = AF_INET;
      server.sin_port = htons( 1100 );
  
      //checks connection
      if (bind(socket_info, (struct sockaddr *)&amp;server, sizeof(server)) &lt; 0) {
      perror("Connection error");
         return 1;
      }
      puts("Bind");
  
      //Receive an incoming message
  if( recv(socket_info, incoming_message, sizeof(incoming_message), 0) &lt; 0) {      
  puts("Received failed");
      return 1;
      }
      puts("Message received");
      puts(incoming_message);
  
  server.sin_addr.s_addr = inet_addr("172.21.8.178");
  
  if (connect(socket_info, (struct sockaddr *)&amp;server, sizeof(server)) &lt; 0) {
      perror("Connection error");
       return 1;
      }
      puts("Connected");
  
  //Sends message back
  char message[100];
  
  printf("Input Message: ");
  fgets(message, 100, stdin);
  
  if(send(socket_info, message, strlen(message), 0) &lt;0) {        
  perror("Send failed");
      return 1;
      }
      puts("Message Sent");
  
  close(socket_info);
  }
  </code></pre>