Click here to Skip to main content
15,894,825 members
Articles / Programming Languages / C#

Windows TCP Tunnel

Rate me:
Please Sign up or sign in to vote.
4.53/5 (26 votes)
12 Jul 20065 min read 140.5K   4.8K   51  
This project shows how to forward TCP sockets from one machine to another.
using System;
using System.Collections;

namespace WinTunnel
{
	/// <summary>
	/// Summary description for ConnectionManager.
	/// </summary>
	public class ConnectionManager
	{
		private ArrayList m_connections= null;

		private static ConnectionManager m_mgr = null;
		private static int m_connCount = 0;

		private static int m_releaseCount = 0;

		private Logger logger;

		private ConnectionManager()
		{
			logger = Logger.getInstance();

			logger.info("ConnectionManager object instantiated.");
			m_connections = ArrayList.Synchronized( new ArrayList());
		}

		public static ConnectionManager getInstance()
		{
			if (m_mgr == null)
			{
				m_mgr = new ConnectionManager();
			}
			return m_mgr;
		}

		public ProxyConnection getConnection()
		{	
			ProxyConnection conn = null;
			lock(this)
			{
				logger.info("Allocating ProxyConnection#{0} for new connection.", m_connCount);
				conn = new ProxyConnection(); //create a new one
				
				m_connections.Add(conn);
				conn.connNumber = m_connCount++;
			}
			return conn;
		}

		public bool Release(ProxyConnection conn)
		{
			m_releaseCount++;

			if (conn.clientSocket != null)
			{
				logger.info("[{0}] Releasing ProxyConnection#{1}: Client {2}, Server {3}.",
					conn.serviceName, conn.connNumber, 
					conn.clientSocket.RemoteEndPoint.ToString(),
					conn.serverEP.ToString());
			}
			else
			{
				logger.info("[{0}] Releasing ProxyConnection#{1}: Server {2}.", 
					conn.serviceName, conn.connNumber, 
					conn.serverEP);
			}

			conn.disconnect();
			m_connections.Remove(conn);

			if (m_releaseCount%100 == 0)
			{
				logger.info("Process is currently using {0} bytes of memory.", System.GC.GetTotalMemory(true));
			}

			return true;
		}

		public bool shutdown()
		{
			logger.info("There are {0} connections in the Connection List.", m_connections.Count);

			foreach (ProxyConnection conn in m_connections)
			{
				logger.info("[{0}] Disconnecting conn#{1}...", conn.serviceName, conn.connNumber);
				conn.disconnect();
			}
			m_connections.Clear(); //remove from the array list
			return true;
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions