//******************************************************************// // Remove a connection to the list of connections in a threadsafe // // way. // //******************************************************************// public void RemoveConnection(ThreadConnection connection) { //*******************************************************// // To assist with debugging only wait one second (which // // is a loooong time) and log a console message if timed // // out. // //*******************************************************// bool timedOut = !sem.WaitOne(1000); if (timedOut) { Console.WriteLine("Wait timed out removing connection"); } listConnections.Remove(connection); try { sem.Release(); } catch (Exception e) { } Console.Write("Object removed from list of connections. List now contains "); Console.Write(listConnections.Count); Console.WriteLine(" connections."); }
//******************************************************************// // Add a new connection to the list of connections in a threadsafe // // way. // //******************************************************************// public void AddConnection(ThreadConnection newConnection) { //*******************************************************// // To assist with debugging only wait one second (which // // is a loooong time) and log a console message if timed // // out. // //*******************************************************// bool timedOut = sem.WaitOne(1000); if (timedOut) { Console.WriteLine("Wait timed out adding new connection"); } listConnections.Add(newConnection); sem.Release(); }
//******************************************************************// // Start the sort of proxy. // //******************************************************************// private void StartProxy() { //**************************************************************// // Try to create a listening socket. // //**************************************************************// try { listeningSocket = new TcpListener(System.Net.IPAddress.Any, port); listeningSocket.Start(); Console.WriteLine("Listening socket started"); //Why don't these echo! } catch (Exception ee) { Console.WriteLine(ee.Message, "Error opening listening socket"); return; } //**************************************************************// // This next bit just prints out the IP address and the host. // // There must be a neater way to do this - Nigel. // //**************************************************************// Console.Out.WriteLine("Listener running"); localHostInfo = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName()); foreach (IPAddress address in localHostInfo.AddressList) { Console.WriteLine("I have Ip address " + address.ToString()); } //**************************************************************// // Sit and wait until something attempts to connect to us. // // // // We block (i.e. wait) on the listeningSocket.AcceptSocket(); // // When it does, we get passed a new connection. // // // // This run() method (I have called it run() to make C# look // // like Java) You will find that this form (Thread) won't // // respond wo any Windows events (try it), as it is blocking in // // the code below. // //**************************************************************// while (true) { connection = listeningSocket.AcceptSocket(); //***********************************************************// //We have now accepted a connection. // // // //There are several ways to do this next bit. Here I make a// //network stream and use it to create two other streams, an // //input and an output stream. Life gets easier at that // //point. // //***********************************************************// ThreadConnection threadConnection = new ThreadConnection(connection, this); //***********************************************************// // Create a new Thread to manage the connection that receives// // data. If you are a Java programmer, this looks like a // // load of hokum cokum.. // //***********************************************************// Thread threadRunner = new Thread(new ThreadStart(threadConnection.run)); threadRunner.Start(); Console.WriteLine("Created new connection class"); } }