private void ThreadLoop() { while (DateTime.Now < end) { try { Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); s.Connect(new IPEndPoint(IPAddress.Loopback, 8081)); SocketHelper.WriteMessage(s, "Garry"); SocketHelper.ReadMessage(s); s.Shutdown(SocketShutdown.Both); s.Close(); SocketHelper.Increment(); } catch (SocketException se) { Console.WriteLine(se); Console.ReadLine(); // local address at e } Thread.Sleep(threadWait); } }
private static void ThreadLoop() { while (true) { Socket s = (Socket)queue.Dequeue(); SocketHelper.ProcessConnection(s); } }
public static void Run() { Socket acceptingSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); acceptingSocket.Bind(new IPEndPoint(IPAddress.Any, 8081)); acceptingSocket.Listen(50); while (true) { Socket s = acceptingSocket.Accept(); SocketHelper.ProcessConnection(s); } }
// Process the request from an accepted socket public static void ProcessConnection(object o) { // Had to have object to make signature compatible with WaitCallback Socket s = (Socket)o; // get the message string name = (string)SocketHelper.ReadMessage(s); // process the message System.Threading.Thread.Sleep(500); // simulate the database work // send the response SocketHelper.WriteMessage(s, "Goodbye " + name); // client will close the socket SocketHelper.ReadMessage(s); // will be null s.Shutdown(SocketShutdown.Both); s.Close(); if (rc != null) { Increment(); } }
/// <summary>The main entry point for the application.</summary> static void Main(string[] args) { int workerThreads; int completionPortThreads; ThreadPool.GetMaxThreads(out workerThreads, out completionPortThreads); Console.WriteLine("Max threads = {0}, {1}", workerThreads, completionPortThreads); SocketHelper.StartRateCounter(TimeSpan.FromSeconds(1)); char mode = (args[0].ToLower())[0]; switch (mode) { case 'c': Client client = new Client(100, 200, TimeSpan.FromSeconds(60)); client.Run(); break; case 's': SingleThreadedServer.Run(); break; case 't': ThreadPoolServer.Run(); break; case 'b': BlockingQueueServer.Run(100); break; default: Console.WriteLine("Expected c|s|t|b"); return; } }