title: TCP and UDP interference
body: <p>I have coded a server-client (two programs) structure which does work properly... only when TCP is used.
  My idea was to use TCP to do text chat transfer (udp is unreliable), but to use UDP for the game packets (yes it is some sort of action game with 30 fps, so I need UDP). </p>
  
  <p>However, when I established a connection with TCP in my client process, I start sending UDP packets to and receive from the server program. The client uses non-blocking sockets, UDP and TCP in a single thread. No multi-threading here, I do not really like that idea if you can implement it in one process too. </p>
  
  <p>However, the problem is that is seems as if the UDP packets take more than 5 seconds to arrive from client to server: they do arrive in most cases (I repeat sendto until I received a packet from the server indicating successful UDP transmission) but it takes far too long. The only problem I can imagine in the structure is the fact that I use TCP and UDP at the same time. </p>
  
  <p>Note that I probably use different ports (sendto lets the OS bind to a port) and I ran the client and server on the same machine. I read somewhere that every process can only send one packet at a time; if that is the case, could that be the culprit of these negative experiences with UDP?</p>
  
  <p>Code of server sending a game frame to client.</p>
  
  <pre><code>void SendGameData()
  {
      unsigned char i, a, c;
      CBytes cont;
      CLoops(i, 0, app.clients)
      {
      if (client[i].step != 4)
          continue;
      // Send basic data
      tempbuf[0] = 0;
      tempbuf[1] = game.players;
      tempbuf[2] = client[i].player;
      core.CSendTo(net.udp, &amp;client[i].udpaddr, tempbuf, 3);
      // Send player positions
      CLoops(a, 0, game.players)
      {
          tempbuf[0] = a + 1;
          c = 1;
          strcpy(tempbuf + c, player[a]-&gt;name);
          c += strlen(player[a]-&gt;name) + 1;
          cont.value = player[a]-&gt;obj.pos.x;
          tempbuf[c++] = cont.bytes[0];
          tempbuf[c++] = cont.bytes[1];
          tempbuf[c++] = cont.bytes[2];
          tempbuf[c++] = cont.bytes[3];
          cont.value = player[a]-&gt;obj.pos.y;
          tempbuf[c++] = cont.bytes[0];
          tempbuf[c++] = cont.bytes[1];
          tempbuf[c++] = cont.bytes[2];
          tempbuf[c++] = cont.bytes[3];
          cont.value = player[a]-&gt;dir.x;
          tempbuf[c++] = cont.bytes[0];
          tempbuf[c++] = cont.bytes[1];
          tempbuf[c++] = cont.bytes[2];
          tempbuf[c++] = cont.bytes[3];
          cont.value = player[a]-&gt;dir.y;
          tempbuf[c++] = cont.bytes[0];
          tempbuf[c++] = cont.bytes[1];
          tempbuf[c++] = cont.bytes[2];
          tempbuf[c++] = cont.bytes[3];
          cont.value = player[a]-&gt;angle;
          tempbuf[c++] = cont.bytes[0];
          tempbuf[c++] = cont.bytes[1];
          tempbuf[c++] = cont.bytes[2];
          tempbuf[c++] = cont.bytes[3];
          tempbuf[c++] = player[a]-&gt;defeated;
          tempbuf[c++] = player[a]-&gt;health;
          /*player[game.players]-&gt;normalspeed = true;
          if (gmode[gdata.modeprofile].mode == M_MATCH &amp;&amp; !gmode[gdata.modeprofile].timeorstock)
              player[game.players]-&gt;lives = gmode[gdata.modeprofile].stock;
          player[game.players]-&gt;score = 0;
          player[game.players]-&gt;wait = 0;
          player[game.players]-&gt;ammo[WP_HMG] = 0;
          player[game.players]-&gt;ammo[WP_CANNON] = 10;
          player[game.players]-&gt;ammo[WP_AUTO] = 15;
          player[game.players]-&gt;ammo[WP_ART] = 0;
          player[game.players]-&gt;ammo[WP_LMG] = 50;
          player[game.players]-&gt;ammo[WP_MINI] = 0;
          player[game.players]-&gt;ammo[WP_AT] = 0;
          player[game.players]-&gt;weapon = WP_AUTO;
          player[game.players]-&gt;doubledamage = 0;
          player[game.players]-&gt;speedboost = 0;*/
          core.CSendTo(net.udp, &amp;client[i].udpaddr, tempbuf, c);
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Client code of receiving the game state from server within a frame time limit:</p>
  
  <pre><code>int r;
  char k;
  CBytes cont;
  memset(&amp;cont, 0, sizeof(CBytes));
  unsigned char c, count = 0;
  CByte8u timez = core.CGetTime() + mswait;
  while (true)
  {
      r = core.CRecvFrom(net.udp, NULL, net.tempbuf, NET_BUFSIZE);
      if (r &gt; 0)
      {
          if (net.tempbuf[0] == 0) // Basics
          {
              k = net.tempbuf[1] - (char)game.players;
              if (k &gt; 0)
              {
                  CLoops(c, 0, (unsigned char)k)
                      SpawnPlayer();
              }
              else if (k &lt; 0)
              {
                  CLoops(c, 0, (unsigned char)k)
                      DespawnPlayer(game.players - 1);
              }
              game.you = net.tempbuf[2];
              count |= 0x01;
          }
          else
          {
              c = 0;
              k = net.tempbuf[0] - 1;
              strcpy(player[k]-&gt;name, net.tempbuf);
              c += strlen(player[k]-&gt;name) + 1;
              cont.bytes[0] = net.tempbuf[c++];
              cont.bytes[1] = net.tempbuf[c++];
              cont.bytes[2] = net.tempbuf[c++];
              cont.bytes[3] = net.tempbuf[c++];
              player[k]-&gt;obj.pos.x = cont.value;
              cont.bytes[0] = net.tempbuf[c++];
              cont.bytes[1] = net.tempbuf[c++];
              cont.bytes[2] = net.tempbuf[c++];
              cont.bytes[3] = net.tempbuf[c++];
              player[k]-&gt;obj.pos.y = cont.value;
              cont.bytes[0] = net.tempbuf[c++];
              cont.bytes[1] = net.tempbuf[c++];
              cont.bytes[2] = net.tempbuf[c++];
              cont.bytes[3] = net.tempbuf[c++];
              player[k]-&gt;dir.x = cont.value;
              cont.bytes[0] = net.tempbuf[c++];
              cont.bytes[1] = net.tempbuf[c++];
              cont.bytes[2] = net.tempbuf[c++];
              cont.bytes[3] = net.tempbuf[c++];
              player[k]-&gt;dir.y = cont.value;
              cont.bytes[0] = net.tempbuf[c++];
              cont.bytes[1] = net.tempbuf[c++];
              cont.bytes[2] = net.tempbuf[c++];
              cont.bytes[3] = net.tempbuf[c++];
              player[k]-&gt;angle = cont.value;
              player[k]-&gt;defeated = (bool)net.tempbuf[c++];
              player[k]-&gt;health = net.tempbuf[c];
              player[k]-&gt;weapon = WP_AUTO;
              count |= 0x02;
          }
      }
      if (count == 3)
          break;
      else if (r == -2)
          return false;
      else if (core.CGetTime() &gt; timez)
      {
          strcpy(core.inerr, "UDP session timed out.");
          return false;
      }
  }
  return true;
  </code></pre>
  
  <p>These processes happen simultaneously on two separate processes, every frame takes about 20 ms.</p>