title: High-Traffic C# UDP Networking
body: <p>I am making a game which demands great traffic of data like Positions(2D) and many other data.
  I am using a very simple class to help me listening on 8080 port(UDP), and a method to send datagram :</p>
  
  <pre><code>public static void SendToHostUDP(string Msg)
  {
      UdpClient udpClient = new UdpClient();
      udpClient.Connect(Main.HostIP, 8080);
      byte[] sdBytes = Encoding.ASCII.GetBytes(Msg);
      udpClient.BeginSend(sdBytes, sdBytes.Length, CallBack, udpClient);
      Main.UDPout += sdBytes.Length / 1000f;
  }
  public static void SendToClientUDP(string Msg, IPAddress ip)
  {
      UdpClient udpClient = new UdpClient();
      udpClient.Connect(ip, 8080);
      byte[] sdBytes = Encoding.ASCII.GetBytes(Msg);
      udpClient.BeginSend(sdBytes, sdBytes.Length, CallBack, udpClient);
      Main.UDPout += sdBytes.Length / 1000f;
  }
  public static void CallBack(IAsyncResult ar)
  {
  
  }
  </code></pre>
  
  <p>The listener class is just a very simple one:</p>
  
  <pre><code>public class NetReciever
  {
      public TcpListener tcpListener;
      public Thread listenThread;
      private Action actionToPerformTCP;
      private Action actionToPerformUDP;
      public UdpClient udpClient;
      public Thread UDPThread;
      TimerAction UDPPacketsCounter;
      int UDPPacketsCounts;
      private BackgroundWorker bkgUDPListener;
      string msg;
      public NetReciever(IPAddress IP)
      {
          this.tcpListener = new TcpListener(IPAddress.Any, 25565);
          this.udpClient = new UdpClient(8080);
          this.UDPThread = new Thread(new ThreadStart(UDPListen));
          this.listenThread = new Thread(new ThreadStart(ListenForClients));
          this.listenThread.Start();
          UDPPacketsCounter = new TimerAction(CountUDPPackets, 1000, false);
          this.UDPThread.Start();
      }
      public void CountUDPPackets()
      {
          UDPPacketsCounts = 0;
      }
      public void Abort()
      {
          UDPThread.Abort();
          udpClient.Close();
          listenThread.Abort();
          tcpListener.Stop();
      }
      public void UDPListen()
      {
          while (true)
          {
              IPEndPoint RemoteIPEndPoint = new IPEndPoint(IPAddress.Any, 0);
              byte[] receiveBytesUDP = udpClient.Receive(ref RemoteIPEndPoint);
              if (receiveBytesUDP != null)
              {
                  UDPPacketsCounts++;
                  Main.UDPin += receiveBytesUDP.Length / 1000f;
              }
              if (Main.isHost)
              {
                  Main.Server.processUDP(Encoding.ASCII.GetString(receiveBytesUDP), RemoteIPEndPoint.Address.ToString());
              }
              else
              {
                  if (RemoteIPEndPoint.Address.ToString() == Main.HostIP)
                  {
                      Program.game.ProcessUDP(Encoding.ASCII.GetString(receiveBytesUDP));
                  }
              }
          }
      }
  </code></pre>
  
  <p>So basically when there is 1 player, there will be approximately 60packet/s IN and 60 packets/s out.</p>
  
  <p>It acts like this:</p>
  
  <ol>
  <li>Listen for packets.</li>
  <li>Validate the packets.</li>
  <li>Process the packets data -> Like storing some of the positions, sending back some packets...</li>
  </ol>
  
  <p>So it just loop like this.</p>
  
  <p>And the problems are here:</p>
  
  <p>Firstly, When there are 2 players(Host+ 1Client), there will be some significant FPS drops at some point, and the host will experience stuttering of all audios(like in blue screen) for a moment.</p>
  
  <p>Secondly, when there are 2+ players(Host + 1+Client), the Host FPS will drop to 5-20, and it will lag+lag+lag+lag but not freezing.</p>
  
  <p>I have read some articles about async, and this is already threaded?</p>
  
  <p>And also <code>BeginRecieve</code> and <code>EndRecieve</code>, I don't really understand how and why do I need to use it.</p>
  
  <p>Could someonekindly provide some examples to explain how to process these kinds of data and send/recieve packets please? I don't really want to use libraries because I want to know what is going on.</p>
  
  <p>P.S: How do the networking system in Terraria works? It uses TCP but it is smooth! How?<br>
  PSS: What is buffering? Why do i need to set buffering and how? What does it changes?<br>
  PSSS: I think there are something to be tuned and changed in sending the packets, because it just look so simple.</p>