Connecting Tech Pros Worldwide Forums | Help | Site Map

Creating A Packet Filter.

Newbie
 
Join Date: Oct 2008
Posts: 6
#1: Jul 8 '09
Ok i have never before tried networking in my programming, so this is the first time i have tried anything like this. But i am trying to make an application which acts as a medium between a server and client, originally the client would communicate directly to the server, but in this case i am trying to filter the packets and remove the ones i don't want. So i created a packet filtering class, which accepts a connection from the client, and connects to the server, i then tried using two separate threads to send the packets received from the server to the client, and receive the packets from the client to send to the server.

So effectively(i have not tried to filter packets yet) the client and server should be communicating exactly the same, except through my application, however this is not the case, and with my lack of experience in sockets i cannot figure out why.

Some good things, the client connects to my application when i click login, however it then freezes waiting for the return packet from the server which i must never get.

Here is the class:
Expand|Select|Wrap|Line Numbers
  1. public static class PacketFilter
  2.     {
  3.         static int BufferSize = 4096;
  4.  
  5.         static Server RedirectToServer;
  6.  
  7.         static bool ConnectingToClient = false;
  8.         static Socket ClientConnection;
  9.         static byte[] RecievedFromClient = new byte[BufferSize];
  10.  
  11.         static bool ConnectingToServer = false;
  12.         static Socket ServerConnection;
  13.         static byte[] RecievedFromServer = new byte[BufferSize];
  14.  
  15.         static Thread ClientConnectionThread = new Thread(new ThreadStart(ClientConnectionThread_Tick));
  16.         static Thread ServerConnectionThread = new Thread(new ThreadStart(ServerConnectionThread_Tick));
  17.  
  18.         public static void Start(Server server)
  19.         {
  20.             RedirectToServer = server;
  21.  
  22.             ClientConnectionThread.Start();
  23.             ServerConnectionThread.Start();
  24.  
  25.             ClientConnect(true);
  26.             ServerConnect(true);
  27.         }
  28.  
  29.         public static void End()
  30.         {
  31.             try
  32.             {
  33.                 ClientConnection.Close();
  34.                 ServerConnection.Close();
  35.             }
  36.             catch (Exception e)
  37.             {
  38.                 MessageBox.Show("Failed to close sockets, " + e.Message);
  39.             }
  40.         }
  41.  
  42.         public static void ClientConnect(bool connect)
  43.         {
  44.             ConnectingToClient = true;
  45.  
  46.             ClientConnection = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  47.             IPEndPoint ep = new IPEndPoint(Dns.GetHostEntry("localhost").AddressList[0], (int)Main.UserInterfaceSettings.ClientPacketListenPort);
  48.             try
  49.             {
  50.                 ClientConnection.Bind(ep);
  51.                 ClientConnection.Listen(5);
  52.                 ClientConnection.BeginAccept(ClientConnect_Complete, ClientConnection);
  53.             }
  54.             catch (Exception) { ConnectingToClient = false; }
  55.         }
  56.  
  57.         public static void ClientConnect_Complete(IAsyncResult sock)
  58.         {
  59.             try
  60.             {
  61.                 ClientConnection = ClientConnection.EndAccept(sock);
  62.             }
  63.             catch (Exception) { }
  64.             ConnectingToClient = false;
  65.         }
  66.  
  67.         public static void ServerConnect(bool connect)
  68.         {
  69.             ConnectingToServer = true;
  70.  
  71.             ServerConnection = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  72.             IPEndPoint ep = new IPEndPoint(RedirectToServer.ReturnServerAddress(), (int)RedirectToServer.ReturnPort());
  73.             try
  74.             {
  75.                 ServerConnection.BeginConnect(ep, ServerConnect_Complete, ServerConnection);
  76.             }
  77.             catch (Exception) { ConnectingToServer = false; }
  78.         }
  79.  
  80.         public static void ServerConnect_Complete(IAsyncResult sock)
  81.         {
  82.             try { ServerConnection.EndConnect(sock); }
  83.             catch (Exception) {  }
  84.             ConnectingToServer = false;
  85.         }
  86.  
  87.         public static void ClientConnectionThread_Tick()
  88.         {
  89.             while (!Program.EndProgram)
  90.             {
  91.                 if (ClientConnection != null)
  92.                 {
  93.                     if (ClientConnection.Connected)
  94.                     {
  95.                         try
  96.                         {
  97.                             ClientConnection.BeginSend(RecievedFromServer, 0, RecievedFromServer.Length, SocketFlags.None, ClientConnectionSend, ClientConnection);
  98.                             ClientConnection.BeginReceive(RecievedFromClient, 0, RecievedFromClient.Length, SocketFlags.None, ClientConnectionRecieve, ClientConnection);
  99.                         }
  100.                         catch (Exception) { }
  101.                     }
  102.                     else
  103.                         if (!ConnectingToClient)
  104.                             ClientConnect(true);
  105.                 }
  106.                 else
  107.                     if (!ConnectingToClient)
  108.                         ClientConnect(true);
  109.  
  110.                 Thread.Sleep(1);
  111.             }
  112.         }
  113.  
  114.         public static void ClientConnectionRecieve(IAsyncResult sock)
  115.         {
  116.             try
  117.             {
  118.                 ClientConnection.EndReceive(sock);
  119.             }
  120.             catch (Exception) { }
  121.         }
  122.  
  123.         public static void ClientConnectionSend(IAsyncResult sock)
  124.         {
  125.             try
  126.             {
  127.                 ClientConnection.EndSend(sock);
  128.                 RecievedFromServer = new byte[BufferSize];
  129.             }
  130.             catch (Exception) { }
  131.         }
  132.  
  133.         public static void ServerConnectionThread_Tick()
  134.         {
  135.             while (!Program.EndProgram)
  136.             {
  137.                 if (ServerConnection != null)
  138.                 {
  139.                     if (ServerConnection.Connected)
  140.                     {
  141.                         try
  142.                         {
  143.                             ServerConnection.BeginSend(RecievedFromClient, 0, RecievedFromClient.Length, SocketFlags.None, ServerConnectionSend, ServerConnection);
  144.                             ServerConnection.BeginReceive(RecievedFromServer, 0, RecievedFromServer.Length, SocketFlags.None, ServerConnectionRecieve, ServerConnection);
  145.                         }
  146.                         catch (Exception) { }
  147.                     }
  148.                     else
  149.                         if (!ConnectingToServer)
  150.                             ServerConnect(true);
  151.                 }
  152.                 else
  153.                     if (!ConnectingToServer)
  154.                         ServerConnect(true);
  155.  
  156.                 Thread.Sleep(10);
  157.             }
  158.         }
  159.  
  160.         public static void ServerConnectionRecieve(IAsyncResult sock)
  161.         {
  162.             try
  163.             {
  164.                 ServerConnection.EndReceive(sock);
  165.             }
  166.             catch (Exception) { }
  167.         }
  168.  
  169.         public static void ServerConnectionSend(IAsyncResult sock)
  170.         {
  171.             try
  172.             {
  173.                 ServerConnection.EndSend(sock);
  174.                 MessageBox.Show(RecievedFromClient.ToString());
  175.                 RecievedFromClient = new byte[BufferSize];
  176.             }
  177.             catch (Exception) { }
  178.         }
  179.     }
  180.  
Thank you very much for any help.

Newbie
 
Join Date: Oct 2008
Posts: 6
#2: Jul 8 '09

re: Creating A Packet Filter.


I made some changes and now it does a tiny bit of what it is supposed to do.

Expand|Select|Wrap|Line Numbers
  1. public static class PacketFilter
  2.     {
  3.         static int BufferSize = 4096;
  4.  
  5.         static Server RedirectToServer;
  6.  
  7.         static bool ConnectingToClient = false;
  8.         static Socket ClientConnection;
  9.         static byte[] RecievedFromClient = new byte[BufferSize];
  10.  
  11.         static bool ConnectingToServer = false;
  12.         static Socket ServerConnection;
  13.         static byte[] RecievedFromServer = new byte[BufferSize];
  14.  
  15.         static int ThreadSyncA = 0;
  16.         static int ThreadSyncB = 0;
  17.         static Thread ClientConnectionThread = new Thread(new ThreadStart(ClientConnectionThread_Tick));
  18.         static Thread ServerConnectionThread = new Thread(new ThreadStart(ServerConnectionThread_Tick));
  19.  
  20.         public static void Start(Server server)
  21.         {
  22.             RedirectToServer = server;
  23.  
  24.             ClientConnectionThread.Start();
  25.             ServerConnectionThread.Start();
  26.  
  27.             ClientConnect(true);
  28.             ServerConnect(true);
  29.         }
  30.  
  31.         public static void End()
  32.         {
  33.             try
  34.             {
  35.                 ClientConnection.Close();
  36.                 ServerConnection.Close();
  37.             }
  38.             catch (Exception e)
  39.             {
  40.                 MessageBox.Show("Failed to close sockets, " + e.Message);
  41.             }
  42.         }
  43.  
  44.         public static void ClientConnect(bool connect)
  45.         {
  46.             ConnectingToClient = true;
  47.  
  48.             ClientConnection = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  49.             IPEndPoint ep = new IPEndPoint(Dns.GetHostEntry("localhost").AddressList[0], (int)Main.UserInterfaceSettings.ClientPacketListenPort);
  50.             try
  51.             {
  52.                 ClientConnection.Bind(ep);
  53.                 ClientConnection.Listen(5);
  54.                 ClientConnection.BeginAccept(ClientConnect_Complete, ClientConnection);
  55.             }
  56.             catch (Exception) { ConnectingToClient = false; }
  57.         }
  58.  
  59.         public static void ClientConnect_Complete(IAsyncResult sock)
  60.         {
  61.             try
  62.             {
  63.                 ClientConnection = ClientConnection.EndAccept(sock);
  64.             }
  65.             catch (Exception) { }
  66.             ConnectingToClient = false;
  67.         }
  68.  
  69.         public static void ServerConnect(bool connect)
  70.         {
  71.             ConnectingToServer = true;
  72.  
  73.             ServerConnection = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  74.             IPEndPoint ep = new IPEndPoint(RedirectToServer.ReturnServerAddress(), (int)RedirectToServer.ReturnPort());
  75.             try
  76.             {
  77.                 ServerConnection.BeginConnect(ep, ServerConnect_Complete, ServerConnection);
  78.             }
  79.             catch (Exception) { ConnectingToServer = false; }
  80.         }
  81.  
  82.         public static void ServerConnect_Complete(IAsyncResult sock)
  83.         {
  84.             try { ServerConnection.EndConnect(sock); }
  85.             catch (Exception) {  }
  86.             ConnectingToServer = false;
  87.         }
  88.  
  89.         public static void ClientConnectionThread_Tick()
  90.         {
  91.             while (!Program.EndProgram)
  92.             {
  93.                 while (ThreadSyncA > ThreadSyncB)
  94.                     Thread.Sleep(1);
  95.                 RecievedFromClient = new byte[BufferSize];
  96.  
  97.                 if (ClientConnection != null)
  98.                 {
  99.                     if (ClientConnection.Connected)
  100.                     {
  101.                         try
  102.                         {
  103.                             ClientConnection.BeginSend(RecievedFromServer, 0, RecievedFromServer.Length, SocketFlags.None, ClientConnectionSend, ClientConnection);
  104.                             //ClientConnection.BeginReceive(RecievedFromClient, 0, RecievedFromClient.Length, SocketFlags.None, ClientConnectionRecieve, ClientConnection);
  105.  
  106.                             //ClientConnection.Send(RecievedFromServer);
  107.                             int test = 0;
  108.                             test = ClientConnection.Receive(RecievedFromClient);
  109.                             if (test != 0)
  110.                                 MessageBox.Show(test.ToString() + " client");
  111.                         }
  112.                         catch (Exception) { }
  113.                     }
  114.                     else
  115.                         if (!ConnectingToClient)
  116.                             ClientConnect(true);
  117.                 }
  118.                 else
  119.                     if (!ConnectingToClient)
  120.                         ClientConnect(true);
  121.  
  122.                 ThreadSyncA += 1;
  123.                 Thread.Sleep(10);
  124.             }
  125.         }
  126.  
  127.         public static void ClientConnectionRecieve(IAsyncResult sock)
  128.         {
  129.             try
  130.             {
  131.                 ClientConnection.EndReceive(sock);
  132.             }
  133.             catch (Exception) { }
  134.         }
  135.  
  136.         public static void ClientConnectionSend(IAsyncResult sock)
  137.         {
  138.             try { ClientConnection.EndSend(sock); }
  139.             catch (Exception) { }
  140.         }
  141.  
  142.         public static void ServerConnectionThread_Tick()
  143.         {
  144.             while (!Program.EndProgram)
  145.             {
  146.                 while (ThreadSyncB > ThreadSyncA)
  147.                     Thread.Sleep(1);
  148.                 RecievedFromServer = new byte[BufferSize];
  149.  
  150.                 if (ServerConnection != null)
  151.                 {
  152.                     if (ServerConnection.Connected)
  153.                     {
  154.                         try
  155.                         {
  156.                             ServerConnection.BeginSend(RecievedFromClient, 0, RecievedFromClient.Length, SocketFlags.None, ServerConnectionSend, ServerConnection);
  157.                             //ServerConnection.BeginReceive(RecievedFromServer, 0, RecievedFromServer.Length, SocketFlags.None, ServerConnectionRecieve, ServerConnection);
  158.  
  159.                             //ServerConnection.Send(RecievedFromClient);
  160.                             int test = 0;
  161.                             test = ServerConnection.Receive(RecievedFromServer);
  162.                             if (test != 0)
  163.                                 MessageBox.Show(test.ToString() + " server");
  164.                         }
  165.                         catch (Exception) { }
  166.                     }
  167.                     else
  168.                         if (!ConnectingToServer)
  169.                             ServerConnect(true);
  170.                 }
  171.                 else
  172.                     if (!ConnectingToServer)
  173.                         ServerConnect(true);
  174.  
  175.                 ThreadSyncB += 1;
  176.                 Thread.Sleep(10);
  177.             }
  178.         }
  179.  
  180.         public static void ServerConnectionRecieve(IAsyncResult sock)
  181.         {
  182.             try
  183.             {
  184.                 ServerConnection.EndReceive(sock);
  185.             }
  186.             catch (Exception) { }
  187.         }
  188.  
  189.         public static void ServerConnectionSend(IAsyncResult sock)
  190.         {
  191.             try { ServerConnection.EndSend(sock); }
  192.             catch (Exception) { }
  193.         }
  194.     }
Now when i click login on the client it pop's up with '83 client' then when i click ok it pops up with '46 server' then the break point i have put on this line

ClientConnection.BeginSend(RecievedFromServer, 0, RecievedFromServer.Length, SocketFlags.None, ClientConnectionSend, ClientConnection);

is never tripped again, in fact any break point in the Client thread is never tripped again, meaning after the reply from the server is received it freezes somewhere and the thread stops ticking.
Reply