title: Problems binding a UDP socket
body: <p>A project I am currently working on requires that clients contact a server through UDP, then subsequently the server is used to establish a token ring network between the peers. The server collects the IP and port of each client/peer and then stores them into an array. Elements of these arrays are then sent by the server over the network back to peers. The peers then use this information to connect to each other instead of the server. I am having a problem transmitting and receiving messages between peers. Using print statements, I have been able to determine that the address structure contains the correct port and address. I may be dealing with a buffer issue or some kind of timing issue. I have reconfigured my code several times and cannot seem to get it to work. Any tips at this point would be greatly appreciated.</p>
  
  <p><strong>edit -- Using wireshark, I have been able to determine that the message is indeed sent over the network. The program halts in the while loop in the following function. However, no message is received</strong></p>
  
  <pre><code>//Thread function to check for messages on the socket
  void receiveMessages(int socket) {
      long int nBytes = -1;
      char buffer[BUFFERSIZE];
  
      //Wait to receive message from other peers
      while (nBytes &lt;= 0) {
          nBytes = recvfrom(socket, buffer, BUFFERSIZE, 0, NULL, NULL);
      }
  
      //Print the message received
      printf("%s", buffer);
  }
  </code></pre>
  
  <p>Main program</p>
  
  <pre><code>//
  //  runPeer.c
  //  tokenRing
  //
  //  Created by Bryce Hagar on 2/13/17.
  //  Copyright © 2017 Bryce Hagar. All rights reserved.
  //
  
  #include &lt;stdio.h&gt;
  #include "bbpeer.h"
  
  int main(int argc, const char * argv[]){
      //Used for creating the Client-Server socket
      int clientSocket;
  
      //Create a local peer
      Host localPeer = createHost(true, true);
      //Get user information; update local peer
      getUserInfo(localPeer, argc, argv);
      //Configure socket structure with local peer info
      configSockStruct(localPeer, INADDR_ANY, localPort);
  
      //Create a remote server
      Host remoteServer = createHost(false, false);
      //Configure socket structure with remote server info
      configSockStruct(remoteServer, remoteIP, remotePort);
  
      //Create a socket bound to the local peer
      clientSocket = createSocket(localPeer);
  
      //Announce peer's existence to server and receive acceptance
      announceToServer(localPeer, remoteServer, clientSocket);
  
      //Create new remote peer host structure
      Host remotePeer = getPeerInfo(clientSocket);
      freeHost(remoteServer);
  
      //Send messages to peers
      sendMessageToPeer(clientSocket, remotePeer, localPeer);
  
      //Receive messages from other peers
      receiveMessages(clientSocket);
  
      return 0;
  }
  </code></pre>
  
  <p>Peer ADT</p>
  
  <pre><code>//
  //  bbpeer.c
  //  bbpeer
  //
  //  Created by Bryce Hagar on 2/16/17.
  //  Copyright © 2017 Bryce Hagar. All rights reserved.
  //
  
  #include "bbpeer.h"
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;arpa/inet.h&gt;
  #include &lt;pthread.h&gt;
  
  
  #define     BUFFERSIZE      1024
  #define     ACCEPTED        "accepted"
  #define     NEW             "new"
  #define     TEST            "test"
  
  int localPort, remotePort; //User input globals
  const char * remoteIP;
  pthread_t newThread;
  
  /* Get user input from command line */
  void getUserInfo(Host localPeer, int argc, const char *argv[]) {
      int j = 1;
  
      //Error check for input issues
      if (!localPeer-&gt;isPeer) {
          printf("User input is only applicable for peers.\n");
          exit(EXIT_FAILURE);
      }
  
      //Get user input from command line
      if (argc == 6) {
          //Connecting to new token ring (server),
          if (!strcmp(argv[1],"-new"))  {
              localPeer-&gt;newRing = true;
              j++;
          }
      }
  
      //Assign arguments to global variables
      localPort = atoi(argv[j++]);
      remoteIP = argv[j++];
      remotePort = atoi(argv[j]);
  }
  
  //Send announcement to server, and wait for acknoledgement
  void announceToServer(Host localPeer, Host remoteServer, int clientSocket) {
      long int nBytes; //Used to send messages
      char buffer[BUFFERSIZE];
  
      while(!localPeer-&gt;accepted){
  
          //Announce new peer message to server
          memset(buffer, 0, BUFFERSIZE);
          strcpy(buffer, NEW);
          nBytes = strlen(buffer) + 1;
  
          //Send message to server
          sendto(clientSocket,buffer,nBytes,0,(struct sockaddr *)&amp;remoteServer-&gt;sockAddr,
                 remoteServer-&gt;addr_size);
  
          //Receive message from server
          nBytes = recvfrom(clientSocket,buffer,BUFFERSIZE,0,NULL, NULL);
  
          //If accepted, set flag
          if (!strcmp(buffer, ACCEPTED)) {
              localPeer-&gt;accepted = true;
              printf("ACCEPTED BY SERVER\n");//(debug)
          }
      }
  }
  
  //Get info from server message to peers
  Host getPeerInfo(int clientSocket) {
      long int numBytes = -1;
      int port;
      char buffer[BUFFERSIZE];
      char *ip;
  
      //Clear buffer, wait for message from server
      memset(buffer, 0, BUFFERSIZE);
      while (numBytes &lt;= 0) {
          numBytes = recvfrom(clientSocket, buffer, BUFFERSIZE, 0, NULL, NULL);
      }
  
      //Tokenize the message into ip and port
      ip = strtok(buffer, "\n");
      port = atoi(strtok(NULL, "\n"));
  
      //Create a new host struct for the remote peer
      Host remotePeer = createHost(true, false);
      //Configure the socket structure for the new remote peer
      configSockStruct(remotePeer, ip, port);
  
      return remotePeer;
  }
  
  //////////////////////////   Possible Error   //////////////////////////////////////
  ////////////////////////////////////////////////////////////////////////////////////
  /////////// ////////////////////////////////////////////////////////////////////////
  //Send message to remote peer from local peer
  void sendMessageToPeer(int p2pSocket, Host remotePeer, Host localPeer) {
      long int nBytes;
      char buffer[BUFFERSIZE];
      bzero(buffer, BUFFERSIZE);
      strcpy(buffer, TEST);
  
      //Send "test" message
      nBytes = strlen(buffer)+1;
  
      //Send message to the socket(debug)
          sendto(p2pSocket,buffer,nBytes,0,(struct sockaddr *)&amp;remotePeer-&gt;sockAddr,
             remotePeer-&gt;addr_size);
      //Print the ip and port stored in the remote peer host (debug)
      printf("%s:%d\n", inet_ntoa(remotePeer-&gt;sockAddr.sin_addr),
              ntohs(remotePeer-&gt;sockAddr.sin_port));
  }
  
  //Thread function to check for messages on the socket
  void receiveMessages(int socket) {
      long int nBytes = -1;
      char buffer[BUFFERSIZE];
  
      //Wait to receive message from other peers
      while (nBytes &lt;= 0) {
          nBytes = recvfrom(socket, buffer, BUFFERSIZE, 0, NULL, NULL);
      }
  
      //Print the message received
      printf("%s", buffer);
  }
  
  ////////////////////////////////////////////////////////////////////////////////////
  ////////////////////////////////////////////////////////////////////////////////////
  //////////////////////////   Possible Error //////////////////////////////////////
  </code></pre>
  
  <p>Host ADT</p>
  
  <pre><code>//
  //  host.c
  //  bbserver
  //
  //  Created by Bryce Hagar on 2/17/17.
  //  Copyright © 2017 Bryce Hagar. All rights reserved.
  //
  
  #include "host.h"
  
  /* Allocate memory for new host struct,
   assigns variables for peers and servers*/
  Host createHost(bool isPeer, bool isLocal) {
      Host newHost = malloc(sizeof(*newHost));
      newHost-&gt;addr_size = sizeof newHost-&gt;sockAddr;
      newHost-&gt;isPeer = isPeer;
      newHost-&gt;isLocal = isLocal;
      if (isPeer) {
          newHost-&gt;newRing = false;
          newHost-&gt;hasToken = false;
          newHost-&gt;accepted = false;
      }
      return newHost;
  }
  
  void freeHost(Host thisHost) {
      free(thisHost);
  }
  
  /* Configure socket structure */
  void configSockStruct(Host thisHost, const char *ip, int port) {
  
      memset((void *) &amp;thisHost-&gt;sockAddr, 0, (size_t)sizeof(thisHost-&gt;sockAddr));
      thisHost-&gt;sockAddr.sin_family = AF_INET;
      memset(thisHost-&gt;sockAddr.sin_zero, '\0', sizeof thisHost-&gt;
          sockAddr.sin_zero);
      thisHost-&gt;sockAddr.sin_port = htons(port);
  
      if (!thisHost-&gt;isLocal) {
          //Get address from functions parameters
          struct hostent *hostptr = gethostbyname(ip);
          memcpy((void *)&amp;thisHost-&gt;sockAddr.sin_addr, 
              (void *)hostptr-&gt;h_addr, hostptr-&gt;h_length);
      }
      else {
          thisHost-&gt;sockAddr.sin_addr.s_addr = htonl(INADDR_ANY);
      }
  }
  
  int createSocket(Host thisHost) {
      //Create UDP socket
      int newSocket = socket(AF_INET, SOCK_DGRAM, 0);
      bind(newSocket, (struct sockaddr *)&amp;thisHost-&gt;sockAddr, 
          sizeof(thisHost-&gt;sockAddr));
      return newSocket;
  }
  </code></pre>