Showing posts with label Networking. Show all posts
Showing posts with label Networking. Show all posts

Monday, 30 May 2011

TCP Port Listener

I was creating a DMZ and needed a tool to test my port forwarding I didn’t feel like downloading a program so I created this very simple piece of code to just listen on a port of my choice and print when I can make a connection. Only tests TCP (not UDP).

 

Use it as follows:

PortListener <Port>

   1: class Program
   2:    {
   3:        
   4:        static void Main(string[] args)
   5:        {
   6:            new Program().Run(args);
   7:           
   8:        }
   9:  
  10:        private void Run(string[] args)
  11:        {
  12:            int port = Int32.Parse(args[0]);
  13:            Console.WriteLine("Started listening on port: {0}", port);
  14:            var server = new TcpListener(IPAddress.Any, port);
  15:            server.Start();
  16:            server.BeginAcceptSocket(new AsyncCallback(server_onAccept), server);
  17:  
  18:            Console.WriteLine("Press any key to exit...");
  19:            Console.ReadKey();
  20:        }
  21:  
  22:        void server_onAccept(IAsyncResult ar)
  23:        {
  24:            var server = ((TcpListener)ar.AsyncState);
  25:            var socket = server.EndAcceptSocket(ar);
  26:            
  27:            Console.WriteLine(string.Format("Socket {0} Connected",socket.RemoteEndPoint.ToString()));
  28:            server.BeginAcceptSocket(new AsyncCallback(server_onAccept), server);
  29:        }
  30:    }

 

Download the source here:

 

https://github.com/TheWalkingDev/Projects/tree/master/DevTools/PortListener/PortListener

Wednesday, 18 May 2011

IPSwitcher: Quickly switch IPAddress Configuration

Are you like me and use different firewalls and configurations depending on what you are doing,
find it irritating to go through the process of changing your IP, gateway or DNS.
Well I put this tool together to help

It sits in the tray Icon, just right click and select your configuration.
Or enter the configuration where you can make changes,
or configure it directly in the config file:

   1: <string>-Name Gaming -AdapterName RTL8167 -IpAddress 10.0.0.65 -Mask 255.255.255.0 -Gateway 10.0.0.25 -DNS 10.0.0.20</string>
   2: <string>-Name Browsing -AdapterName RTL8167 -IpAddress 10.0.0.65 -Mask 255.255.255.0 -Gateway 10.0.0.20 -DNS 10.0.0.20</string>
   3:  

image





image





image



How I built it:
You need System.Management

 C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\System.Management.dll

Here is how to get a list of adapters on the machine:

   1: public IEnumerable<dynamic> GetAdapters()
   2:         {
   3:             ManagementClass objMC = new ManagementClass(
   4:                 "Win32_NetworkAdapterConfiguration");
   5:             ManagementObjectCollection objMOC = objMC.GetInstances();
   6:             foreach (dynamic objMO in objMOC)
   7:             {
   8:  
   9:                 if (objMO["IPEnabled"])
  10:                 {
  11:                     yield return new Adapter(objMO,
  12:                         objMO["Caption"].ToString(),
  13:                         objMO["ServiceName"].ToString());
  14:                 }
  15:             }
  16:         }

 Then update the adapter:

   1: internal void UpdateAdapter()
   2:         {
   3:             foreach (var adapter in GetAdapters())
   4:             {
   5:                 if (adapter.Name.CompareTo(_adapterName) == 0)
   6:                 {
   7:  
   8:                     ManagementBaseObject objNewIP = null;
   9:                     ManagementBaseObject objSetIP = null;
  10:                     ManagementBaseObject objNewGate = null;
  11:                     objNewIP = adapter.ManagementObject.GetMethodParameters("EnableStatic");
  12:                     objNewGate = adapter.ManagementObject.GetMethodParameters("SetGateways");
  13:  
  14:                     objNewGate["DefaultIPGateway"] = new string[] { _Gateway };
  15:                     objNewGate["GatewayCostMetric"] = new int[] { 1 };
  16:                     //Set IPAddress and Subnet Mask
  17:  
  18:                     objNewIP["IPAddress"] = new string[] { _IpAddress };
  19:                     objNewIP["SubnetMask"] = new string[] { _mask };
  20:  
  21:                     objSetIP = adapter.ManagementObject.InvokeMethod("EnableStatic", objNewIP, null);
  22:                     objSetIP = adapter.ManagementObject.InvokeMethod("SetGateways", objNewGate, null);
  23:                       
  24:                 }
  25:             }
  26:         }

The complete project with source is available here: