title: C++ UDP two way client/server delivery
body: <p>I'm building a UDP server for handling of position updates in a flight simulator (so, the packets will be rapid and guaranteed delivery isn't needed). The server creates and binds a UDP socket as per "session" in the server. The server then communicates the position information from the master to the viewers. This logic works (although, I currently have it coded to echo every message sent to it back to the sender, for testing) on a LAN (I had the server and client setup on the same system to test).</p>
  
  <p>When I moved it to WAN, the client was no longer able to receive messages from the server, but the server received messages from the client. I am afraid it is a firewall/port blocking issue, however, I am coming here in hopes of either finding a different/correct way to do what I'm doing, or instructions on how I can escape the blocking.</p>
  
  <p>The code for the server is as follows:</p>
  
  <pre><code>user newuser;
  ommand = raw_connections[i].sockfd;     
  sockaddr_in sin;
  socklen_t len = sizeof(sin);
  getsockname(raw_connections[i].sockfd, (sockaddr*)&amp;sin, &amp;len); //raw_connections is a vector of sockets that haven't yet authenticated
  newuser.addr.sin_addr = sin.sin_addr;
  newuser.addr.sin_family = sin.sin_family;
  newuser.addr.sin_port = atoi(inp[4].c_str()); //the UDP port as specified by the client
  flights[fpos].users.push_back(newuser); //fpos is the index in the vector
  </code></pre>
  
  <p>The above data is called as a result of a TCP socket between my program's UI and the system (the portion of that will handle user lists, administration, etc. - the UDP port is given to it from the position system, which generates it as follows:)</p>
  
  <pre><code>sockaddr_in service1;
  service1.sin_family = AF_INET;
  service1.sin_addr.s_addr = INADDR_ANY;
  service1.sin_port = htons(0);
  
  server_sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  
  mode = 1;
  ioctlsocket(server_sock, FIONBIO, &amp;mode);
  
  soreuse = 1;
  ioctlsocket(server_sock, SO_REUSEADDR, &amp;soreuse);
  
  
  sockaddr_in sin;
  socklen_t len = sizeof(sin);
  getsockname(server_sock, (sockaddr*)&amp;sin, &amp;len);
  udpport = sin.sin_port; //the value of UDP port here is what's given to the client, and ultimately, the server
  </code></pre>
  
  <p>The sending portion of the server is this:</p>
  
  <pre><code>sockaddr_in addr;
  int sz = sizeof(addr);
  char* buff = new char[1024];            
  int recvdf = recvfrom(gp-&gt;data_socket,buff,1024,NULL,(sockaddr*)&amp;addr,&amp;sz);
  if (recvdf &gt; 0) {
      cout &lt;&lt; "got packet with size " &lt;&lt; recvdf  &lt;&lt; " on port " &lt;&lt; addr.sin_port &lt;&lt; endl;                             
      unsigned sz = gp-&gt;users.size();
      for (unsigned i = 0; i &lt; sz; i++) {
          sendto(gp-&gt;data_socket, buff, recvdf, NULL, (sockaddr*)&amp;gp-&gt;users[i].addr, sizeof(gp-&gt;users[i].addr));
      }
  }
  delete[] buff;
  Sleep(1);
  </code></pre>
  
  <p>Finally, the interaction on the client side is this:</p>
  
  <pre><code>char* buffer = new char[1024];
  int recvd = recvfrom(server_sock, buffer, 1024, NULL, (sockaddr*)&amp;server_addr, &amp;sockaddrsize);
  if (recvd &gt; 0) {
      int i = 282;
  }
  delete[] buffer;
  sendto(server_sock, "test\0", 5, NULL, (sockaddr*)&amp;server_addr, sockaddrsize);
  </code></pre>
  
  <p>Any suggestions to get it working? (The useless variable declared was used as a breakpoint in Visual Studio.)</p>