/// <summary> /// Server starting point /// </summary> public void Start() { _port = Convert.ToInt32(Program.Configuration["Port"]); IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, _port); // Create listening socket, queue size is 5 now. serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); serverSocket.Bind(localEndPoint); serverSocket.Listen(5); _parent.Log("Server started at port " + _port + "."); while (true) { try { // Wait for client clientSocket = serverSocket.Accept(); // Get one, show some info _parent.Log("Client accepted:" + clientSocket.RemoteEndPoint.ToString()); HTTPProcessor hp = new HTTPProcessor(clientSocket, _parent); hp.Process(); } catch (Exception ex) { _parent.Log("Server starting error: " + ex.Message + "\n" + ex.StackTrace); } } }
/// <summary> /// Server starting point /// </summary> public void Start() { try { _port = Convert.ToInt32(Program.Configuration["Port"]); IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, _port); // Create listening socket, queue size is 5 now. serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); serverSocket.Bind(localEndPoint); serverSocket.Listen(5); _parent.Log("Server started at port " + _port + "."); while (true) { clientSocket = serverSocket.Accept(); HTTPProcessor hp = new HTTPProcessor(clientSocket, _parent); Thread t = new Thread(new ThreadStart(hp.Process)); t.Start(); } } catch (Exception ex) { _parent.Log("Server starting error: " + ex.Message + "\n" + ex.StackTrace); } //} }
/// <summary> /// Server starting point /// </summary> public void Start() { while (true) { try { // Create listening socket, queue size is 5 now. IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, _port); serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); serverSocket.Bind(localEndPoint); serverSocket.Listen(5); _parent.Log("Server started at port " + _port + "."); break; } catch (Exception ex) { _parent.Log("Server started unsuccessfully."); _parent.Log(ex.Message); } _port = _port + 1; } while (true) { try { // Wait for client clientSocket = serverSocket.Accept(); // Get one, show some info _parent.Log("Client accepted:" + clientSocket.RemoteEndPoint.ToString()); HTTPProcessor hp = new HTTPProcessor(clientSocket, _parent); // Single thread //hp.Process(); // End single therad //Muliti thread //Thread multiproc = new Thread(new ThreadStart(hp.Process));//create new thread //multiproc.Start();//start thread //End multithread var sectionMin = Program.Configuration.GetSection("MinThread"); //get minthread from config var sectionMax = Program.Configuration.GetSection("MaxThread"); //get maxthread from config int MaxThread = Convert.ToInt32(sectionMax.Value); int MinThread = Convert.ToInt32(sectionMin.Value); ThreadPool.SetMaxThreads(MaxThread, MaxThread); //set Max#worker/IO threadpool ThreadPool.SetMinThreads(MinThread, MinThread); //set Min#worker/IO threadpool ThreadPool.QueueUserWorkItem(new WaitCallback(hp.Process)); //execute the method when threadpool is available by queues } catch (Exception ex) { _parent.Log("Server starting error: " + ex.Message + "\n" + ex.StackTrace); } } }
/// <summary> /// Server starting point /// </summary> public void Start() { while (true) { try { // Create listening socket, queue size is 5 now. IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, _port); serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); serverSocket.Bind(localEndPoint); serverSocket.Listen(5); _parent.Log("Server started at port " + _port + "."); break; } catch (Exception ex) { _parent.Log("Server started unsuccessfully."); _parent.Log(ex.Message); } _port = _port + 1; } while (true) { try { // Wait for client clientSocket = serverSocket.Accept(); // Get one, show some info _parent.Log("Client accepted:" + clientSocket.RemoteEndPoint.ToString()); HTTPProcessor hp = new HTTPProcessor(clientSocket, _parent); // Single thread (Old) //hp.Process(); //This is old single thread that we wont use it now. // End single therad // Multi thread (Old) // Ref. docs.microsoft.com //Thread thread = new Thread(new ThreadStart(hp.Process)); //thread.Start(); // End multi thread // Pool thread (New) // Ref2. https://docs.microsoft.com/en-us/previous-versions/dotnet/articles/ms973903(v=msdn.10) WaitCallback callback; callback = new WaitCallback(hp.Process); // Thanks to these forum for control thread information // Ref3. https://stackoverflow.com/questions/10342057/c-sharp-threadpool-limit-number-of-threads // control thread here ThreadPool.SetMinThreads(1, 1); ThreadPool.SetMaxThreads(256, 256); // control end here ThreadPool.QueueUserWorkItem(callback); // End pool thread } catch (Exception ex) { _parent.Log("Server starting error: " + ex.Message + "\n" + ex.StackTrace); } } }
/// <summary> /// Server starting point /// </summary> public void Start() { //Implement dictionary for keep thread //Dictionary<int, Thread> threads = new Dictionary<int, Thread>(); while (true) { try { _port = Convert.ToInt32(Program.Configuration["Port"]); IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, _port); // Create listening socket, queue size is 5 now. serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); serverSocket.Bind(localEndPoint); serverSocket.Listen(5); _parent.Log("Server started at port " + _port + "."); break; } catch (Exception ex) { _parent.Log("Server can't start"); _parent.Log(ex.Message); } _port = _port + 1; //next port for next thread } while (true) { //Multithreading //if you want to use thread pool that give comment multithreading and uncomment thread pool operate try { // Wait for client clientSocket = serverSocket.Accept(); // Get one, show some info //Implement thread pool ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc)); _parent.Log("Client accepted:" + clientSocket.RemoteEndPoint.ToString()); Thread.Sleep(10000); HTTPProcessor hp = new HTTPProcessor(clientSocket, _parent); //Create thread //Thread thread = new Thread(new ThreadStart(hp.Process)); //Star thread //thread.Start(); //Add thread that execute in thread dictionary //threads.Add(thread.ManagedThreadId,thread); // }catch(ThreadAbortException ex) // { // _parent.Log($"Thread Abort : {ex.Message}"); } catch (Exception ex) { _parent.Log("Server starting error: " + ex.Message + "\n" + ex.StackTrace); } } }
/// <summary> /// Server starting point /// </summary> public void Start() { while (true) { try { // Create listening socket, queue size is 5 now. IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, _port); serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); serverSocket.Bind(localEndPoint); serverSocket.Listen(5); _parent.Log("Server started at port " + _port + "."); break; } catch (Exception ex) { _parent.Log("Server started unsuccessfully."); _parent.Log(ex.Message); } _port = _port + 1; } while (true) { try { // Wait for client clientSocket = serverSocket.Accept(); // Get one, show some info _parent.Log("Client accepted:" + clientSocket.RemoteEndPoint.ToString()); HTTPProcessor hp = new HTTPProcessor(clientSocket, _parent); // Single thread //hp.Process(); /* check thread id */ //Console.Write("Thread ID: "); //Console.WriteLine(Thread.CurrentThread.ManagedThreadId); // End single therad //Multiple Thread //Thread thread = new Thread(new ThreadStart(hp.Process)); //create thread object //thread.Start(); var valuemax = Program.Configuration.GetSection("sizethread"); //get value from sizethread int max = Convert.ToInt32(valuemax.Value); //convert the number in "sizethread" to integer //set treadpool size ThreadPool.SetMaxThreads(max, max); ThreadPool.QueueUserWorkItem(new WaitCallback(hp.Process)); //create thread pool } catch (Exception ex) { _parent.Log("Server starting error: " + ex.Message + "\n" + ex.StackTrace); } } }
/// <summary> /// Server starting point /// </summary> public void Start() { while (true) { try { // Create listening socket, queue size is 5 now. IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, _port); serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); serverSocket.Bind(localEndPoint); serverSocket.Listen(5); _parent.Log("Server started at port " + _port + "."); break; } catch (Exception ex) { _parent.Log("Server started unsuccessfully."); _parent.Log(ex.Message); } _port = _port + 1; } while (true) { try { // Wait for client clientSocket = serverSocket.Accept(); // Get one, show some info _parent.Log("I think Client accepted:" + clientSocket.RemoteEndPoint.ToString()); HTTPProcessor hp = new HTTPProcessor(clientSocket, _parent); if (_parent.threadMode.Equals("single")) { Thread InstanceCaller = new Thread(new ThreadStart(hp.Process)); InstanceCaller.Start(); } else if (_parent.threadMode.Equals("pool")) { WaitCallback callBack = new WaitCallback(poolFunc); ThreadPool.QueueUserWorkItem(callBack, hp); } else { hp.Process(); } } catch (Exception ex) { _parent.Log("Server starting error: " + ex.Message + "\n" + ex.StackTrace); } } }
/// <summary> /// Pool thread function /// </summary> private static void poolFunc(object state) { try { HTTPProcessor hp = (HTTPProcessor)state; hp.Process(); } catch (Exception ex) { Console.Write("Server starting error: " + ex.Message + "\n" + ex.StackTrace); } }
/// <summary> /// Server starting point /// </summary> public void Start() { threads = new Dictionary <int, Thread>(); while (true) { try { // Create listening socket, queue size is 5 now. IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, _port); serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); serverSocket.Bind(localEndPoint); serverSocket.Listen(5); _parent.Log("Server started at port " + _port + "."); break; } catch (Exception ex) { _parent.Log("Server started unsuccessfully."); _parent.Log(ex.Message); } _port = _port + 1; } while (true) { try { // Wait for client clientSocket = serverSocket.Accept(); // Get one, show some info _parent.Log("Client accepted:" + clientSocket.RemoteEndPoint.ToString()); HTTPProcessor hp = new HTTPProcessor(clientSocket, _parent); // Multiple threads Thread thread = new Thread(new ThreadStart(hp.Process)); thread.Start(); thread.Name = $"{clientSocket.RemoteEndPoint}"; threads.Add(thread.ManagedThreadId, thread); // Show active threads foreach (var t in threads) { _parent.Log($"{t.Key}, {t.Value.Name}"); } } catch (ThreadAbortException ex) { _parent.Log($"Thread Aborted: {ex.Message}"); } catch (Exception ex) { _parent.Log("Server starting error: " + ex.Message + "\n" + ex.StackTrace); } } }
/// <summary> /// Server starting point /// </summary> public void Start() { while (true) { try { // Create listening socket, queue size is 5 now. IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, _port); serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); serverSocket.Bind(localEndPoint); serverSocket.Listen(5); _parent.Log("Server started at port " + _port + "."); break; } catch (Exception ex) { _parent.Log("Server started unsuccessfully."); _parent.Log(ex.Message); } _port = _port + 1; } while (true) { try { // Wait for client clientSocket = serverSocket.Accept(); // Get one, show some info _parent.Log("Client accepted:" + clientSocket.RemoteEndPoint.ToString()); HTTPProcessor hp = new HTTPProcessor(clientSocket, _parent); //Thread thread = new Thread(new ThreadStart(hp.Process)); //ref from https://stackoverflow.com/questions/6175868/c-sharp-multi-threading //thread.IsBackground = true; //thread.Start(); ////set maxinum number of worker threads and maximum number of I/O threads size (40,40) ThreadPool.SetMaxThreads(40, 40); //ref from https://stackoverflow.com/questions/444627/c-sharp-thread-pool-limiting-threads ThreadPool.QueueUserWorkItem(hp.Process); //Console.WriteLine("Main thread does some work, then sleeps."); //Thread.Sleep(1000); // Single thread //hp.Process(); // End single therad } catch (Exception ex) { _parent.Log("Server starting error: " + ex.Message + "\n" + ex.StackTrace); } } }
/// <summary> /// Server starting point /// </summary> public void Start() { while (true) { try { // Create listening socket, queue size is 5 now. IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, _port); serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); serverSocket.Bind(localEndPoint); serverSocket.Listen(5); _parent.Log("Server started at port " + _port + "."); break; } catch (Exception ex) { _parent.Log("Server started unsuccessfully."); _parent.Log(ex.Message); } _port = _port + 1; } while (true) { try { // Wait for client clientSocket = serverSocket.Accept(); // Get one, show some info _parent.Log("Client accepted:" + clientSocket.RemoteEndPoint.ToString()); HTTPProcessor hp = new HTTPProcessor(clientSocket, _parent); // MultiThread ThreadStart beginThread = new ThreadStart(hp.Process); Thread threads = new Thread(beginThread); try { threads.Start(); _parent.Log("Thread State : " + threads.ThreadState); } catch (ThreadAbortException ex) { _parent.Log("Thread - caught ThreadAbortException - resetting."); _parent.Log("Exception message: {0}" + ex.Message); threads.Abort(); }; // End MultiThread } catch (Exception ex) { _parent.Log("Server starting error: " + ex.Message + "\n" + ex.StackTrace); } } }
/// <summary> /// Server starting point /// </summary> public void Start() { while (true) { try { // Create listening socket, queue size is 5 now. IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, _port); serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); serverSocket.Bind(localEndPoint); serverSocket.Listen(5); _parent.Log("Server started at port " + _port + "."); break; } catch (Exception ex) { _parent.Log("Server started unsuccessfully."); _parent.Log(ex.Message); } _port = _port + 1; } while (true) { try { // Wait for client clientSocket = serverSocket.Accept(); // Get one, show some info _parent.Log("Client accepted:" + clientSocket.RemoteEndPoint.ToString()); HTTPProcessor hp = new HTTPProcessor(clientSocket, _parent); // Single thread //hp.Process(); // End single therad //Multi thread //Thread thread = new Thread(new ThreadStart(hp.Process)); //thread.Start(); //_parent.Log("Thread #" + thread.ManagedThreadId.ToString() +" : " + thread.ThreadState.ToString()); //End Mult thread //Threads Pool ThreadPool.SetMaxThreads(256, 256); ThreadPool.SetMinThreads(64, 64); ThreadPool.QueueUserWorkItem(new WaitCallback(hp.Process)); //End Threads Pool } catch (Exception ex) { _parent.Log("Server starting error: " + ex.Message + "\n" + ex.StackTrace); } } }
/// <summary> /// Server starting point /// </summary> public void Start() { while (true) { try { // Create listening socket, queue size is 5 now. IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, _port); serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); serverSocket.Bind(localEndPoint); serverSocket.Listen(5); _parent.Log("Server started at port " + _port + "."); //code from https://stackoverflow.com/questions/21155352/get-ip-address-of-client-machine //show IP Client. IPAddress[] addr = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName()).AddressList; _parent.Log("Client IP: : " + addr[1].ToString()); // TcpListener tcpListener = new TcpListener(ipAddress, portNumber); break; } catch (Exception ex) { _parent.Log("Server started unsuccessfully."); _parent.Log(ex.Message); } _port = _port + 1; } while (true) { try { // Wait for client clientSocket = serverSocket.Accept(); // Get one, show some info _parent.Log("Client accepted:" + clientSocket.RemoteEndPoint.ToString()); HTTPProcessor hp = new HTTPProcessor(clientSocket, _parent); ThreadPool.SetMaxThreads(100, 0); //set max values of ThreadPool. ThreadPool.SetMinThreads(1, 0); //set min values of ThreadPool. ThreadPool.QueueUserWorkItem(new WaitCallback(hp.Process)); // Queue the task. // Single thread //hp.Process(); // End single therad } catch (Exception ex) { _parent.Log("Server starting error: " + ex.Message + "\n" + ex.StackTrace); } } }
/// <summary> /// Server starting point /// </summary> public void Start() { _port = Convert.ToInt32(Program.Configuration["Port"]); IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, _port); int Model_server = 1; // Create listening socket, queue size is 5 now. serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); serverSocket.Bind(localEndPoint); serverSocket.Listen(5); _parent.Log("Server started at port " + _port + "."); while (true) { try { // Wait for client clientSocket = serverSocket.Accept(); _parent.Log("Client accepted:" + clientSocket.RemoteEndPoint.ToString()); HTTPProcessor hp = new HTTPProcessor(clientSocket, _parent); // Single thread // hp.Process(); // End single therad switch (Model_server) { case 1: ThreadPool.QueueUserWorkItem(ThreadProc, hp); break; case 2: Thread clientThread = new Thread(hp.Process); clientThread.Start(); break; case 3: hp.Process(); break; } } catch (Exception ex) { _parent.Log("Server starting error: " + ex.Message + "\n" + ex.StackTrace); } } }
/// <summary> /// Server starting point /// </summary> public void Start() { _port = Convert.ToInt32(Program.Configuration["Port"]); IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, _port); // Create listening socket, queue size is 5 now. serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); serverSocket.Bind(localEndPoint); serverSocket.Listen(5); _parent.Log("Server started at port " + _port + "."); RunType runType = RunType.thread; while (true) { try { // Wait for client clientSocket = serverSocket.Accept(); // Get one, show some info _parent.Log("Client accepted:" + clientSocket.RemoteEndPoint.ToString()); HTTPProcessor hp = new HTTPProcessor(clientSocket, _parent); switch (runType) { case RunType.normal: hp.Process(); break; case RunType.thread: Thread thread = new Thread(new ThreadStart(hp.Process)); thread.Start(); break; case RunType.threadPool: ThreadPool.QueueUserWorkItem(ThreadProc, hp); Thread.Sleep(50); break; } } catch (Exception ex) { _parent.Log("Server starting error: " + ex.Message + "\n" + ex.StackTrace); } } }
/// <summary> /// Server starting point /// </summary> public void Start() { while (true) { try { // Create listening socket, queue size is 5 now. IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, _port); serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); serverSocket.Bind(localEndPoint); serverSocket.Listen(5); _parent.Log("Server started at port " + _port + "."); break; } catch (Exception ex) { _parent.Log("Server started unsuccessfully."); _parent.Log(ex.Message); } _port = _port + 1; } while (true) { try { // Wait for client clientSocket = serverSocket.Accept(); _parent.Log("Client IP: " + IPAddress.Parse(((IPEndPoint)clientSocket.RemoteEndPoint).Address.ToString())); //My friend Tune teach me his code is 600611030 he told me to learn from https://docs.microsoft.com/en-us/dotnet/api/system.net.sockets.socket.remoteendpoint?view=netframework-4.7.2 _parent.Log("Client Port: " + (((IPEndPoint)clientSocket.RemoteEndPoint).Port.ToString())); //My friend Tune teach me his code is 600611030 he told me to learn from https://docs.microsoft.com/en-us/dotnet/api/system.net.sockets.socket.remoteendpoint?view=netframework-4.7.2 HTTPProcessor hp = new HTTPProcessor(clientSocket, _parent); // Single thread //hp.Process(); ThreadPool.SetMaxThreads(50, 50);//My friend 600611030 advise me ThreadPool.QueueUserWorkItem(hp.Process); // End single therad } catch (Exception ex) { _parent.Log("Server starting error: " + ex.Message + "\n" + ex.StackTrace); } } }
/// <summary> /// Server starting point /// </summary> public void Start() { while (true) { try { // Create listening socket, queue size is 5 now. IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, _port); serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); serverSocket.Bind(localEndPoint); serverSocket.Listen(5); _parent.Log("Server started at port " + _port + "."); break; } catch (Exception ex) { _parent.Log("Server started unsuccessfully."); _parent.Log(ex.Message); } _port = _port + 1; } while (true) { try { // Wait for client clientSocket = serverSocket.Accept(); // Get one, show some info _parent.Log("Client accepted:" + clientSocket.RemoteEndPoint.ToString()); HTTPProcessor hp = new HTTPProcessor(clientSocket, _parent); var maxval = Program.Configuration.GetSection("MaxThreads"); int max = Convert.ToInt32(maxval.Value); var minval = Program.Configuration.GetSection("MinThreads"); int min = Convert.ToInt32(minval.Value); ThreadPool.QueueUserWorkItem(hp.Process); ThreadPool.SetMaxThreads(max, max); } catch (Exception ex) { _parent.Log("Server starting error: " + ex.Message + "\n" + ex.StackTrace); } } }
/// <summary> /// Server starting point /// </summary> public void Start() { while (true) { try { // Create listening socket, queue size is 5 now. IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, _port); serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); serverSocket.Bind(localEndPoint); serverSocket.Listen(5); _parent.Log("Server started at port " + _port + "."); break; } catch (Exception ex) { _parent.Log("Server started unsuccessfully."); _parent.Log(ex.Message); } _port = _port + 1; } while (true) { try { // Wait for client clientSocket = serverSocket.Accept(); // Get one, show some info _parent.Log("Client accepted:" + clientSocket.RemoteEndPoint.ToString()); HTTPProcessor hp = new HTTPProcessor(clientSocket, _parent); // Single thread ThreadPool.SetMaxThreads(80, 80); ThreadPool.QueueUserWorkItem(hp.Process); //https://docs.microsoft.com/en-us/dotnet/api/system.threading.threadpool?view=netframework-4.7.2 //https://docs.microsoft.com/en-us/dotnet/api/system.threading.threadpool.setmaxthreads?view=netframework-4.7.2 //https://docs.microsoft.com/en-us/dotnet/api/system.threading.threadpool.queueuserworkitem?view=netframework-4.7.2 // End single therad } catch (Exception ex) { _parent.Log("Server starting error: " + ex.Message + "\n" + ex.StackTrace); } } }
/// <summary> /// Server starting point /// </summary> public void Start() { // Start the listener try { if (!HttpListener.IsSupported) { Console.WriteLine("Http Listener is not supported"); return; } string[] prefixes = { "http://*:" + Program.Configuration["Port"] + "/" }; listener = new HttpListener(); foreach (string s in prefixes) { listener.Prefixes.Add(s); } listener.Start(); _parent.Log("Server started"); } catch (Exception ex) { _parent.Log(ex.ToString()); return; } // Run the processing loop while (true) { try { // Wait for client HttpListenerContext context = listener.GetContext(); // Get one, show some info _parent.Log("Client accepted:" + context.Request.RemoteEndPoint.ToString()); HTTPProcessor hp = new HTTPProcessor(context, _parent); Thread thread = new Thread(new ThreadStart(hp.Process)); id++; thread.Start(); } catch (Exception ex) { _parent.Log("Server starting error: " + ex.Message + "\n" + ex.StackTrace); } } }
/// <summary> /// Server starting point /// </summary> public void Start() { while (true) { try { // Create listening socket, queue size is 5 now. IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, _port); serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); serverSocket.Bind(localEndPoint); serverSocket.Listen(5); _parent.Log("Server started at port " + _port + "."); break; } catch (Exception ex) { _parent.Log("Server started unsuccessfully."); _parent.Log(ex.Message); } _port = _port + 1; } while (true) { try { // Wait for client clientSocket = serverSocket.Accept(); // Get one, show some info _parent.Log("Client accepted:" + clientSocket.RemoteEndPoint.ToString()); HTTPProcessor hp = new HTTPProcessor(clientSocket, _parent); // Single thread //Thread thread1 = new Thread(new ThreadStart(hp.Process)); //thread1.Start(); ThreadPool.SetMaxThreads(40, 40); //set maxinum number of worker threads and maximum number of asynchronous I/O threads in the thread pool 40 and 40 ThreadPool.QueueUserWorkItem(hp.Process); // End single therad } catch (Exception ex) { _parent.Log("Server starting error: " + ex.Message + "\n" + ex.StackTrace); } } }
private void MainLoopSingleThread() { while (true) { try { // Wait for client clientSocket = serverSocket.Accept(); // Get one, show some info _parent.Log("Client accepted:" + clientSocket.RemoteEndPoint.ToString()); HTTPProcessor hp = new HTTPProcessor(clientSocket, _parent); hp.Process(); } catch (Exception ex) { _parent.Log("Server starting error: " + ex.Message + "\n" + ex.StackTrace); } } }
private void MainLoopThreadPool() { while (true) { try { // Wait for client clientSocket = serverSocket.Accept(); // Get one, show some info _parent.Log("Client accepted:" + clientSocket.RemoteEndPoint.ToString()); HTTPProcessor hp = new HTTPProcessor(clientSocket, _parent); TaskInfo ti = new TaskInfo(hp); ThreadPool.QueueUserWorkItem(ThreadProc, ti); } catch (Exception ex) { _parent.Log("Server starting error: " + ex.Message + "\n" + ex.StackTrace); } } }
/// <summary> /// Server starting point /// </summary> public void Start() { List <Thread> Threadlist = new List <Thread>(); while (true) { try { // Create listening socket, queue size is 5 now. IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, _port); serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); serverSocket.Bind(localEndPoint); serverSocket.Listen(5); _parent.Log("Server started at port " + _port + "."); break; } catch (Exception ex) { _parent.Log("Server started unsuccessfully."); _parent.Log(ex.Message); } _port = _port + 1; } while (true) { try { // Wait for client clientSocket = serverSocket.Accept(); // Get one, show some info _parent.Log("Client accepted:" + clientSocket.RemoteEndPoint.ToString()); HTTPProcessor hp = new HTTPProcessor(clientSocket, _parent); Threadlist.Add(new Thread(() => hp.Process())); Threadlist[Threadlist.Count].Start(); } catch (Exception ex) { _parent.Log("Server starting error: " + ex.Message + "\n" + ex.StackTrace); } } }
/// <summary> /// Server starting point /// </summary> public void Start() { while (true) { try { // Create listening socket, queue size is 5 now. IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, _port); serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); serverSocket.Bind(localEndPoint); serverSocket.Listen(5); _parent.Log("Server started at port " + _port + "."); break; } catch (Exception ex) { _parent.Log("Server started unsuccessfully."); _parent.Log(ex.Message); } _port = _port + 1; } while (true) { try {//1039 teah me // Wait for client clientSocket = serverSocket.Accept(); // Get one, show some info _parent.Log("Client accepted:" + clientSocket.RemoteEndPoint.ToString()); HTTPProcessor hp = new HTTPProcessor(clientSocket, _parent); ThreadPool.SetMaxThreads(100, 0); //set max values of ThreadPool. ThreadPool.SetMinThreads(1, 0); //set min values of ThreadPool. ThreadPool.QueueUserWorkItem(new WaitCallback(hp.Process)); // Queue the task. } catch (Exception ex) { _parent.Log("Server starting error: " + ex.Message + "\n" + ex.StackTrace); } } }
/// <summary> /// Server starting point /// </summary> public void Start() { _port = Convert.ToInt32(Program.Configuration["Port"]); IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, _port); // Create listening socket, queue size is 5 now. serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); serverSocket.Bind(localEndPoint); serverSocket.Listen(5); _parent.Log("Server started at port " + _port + "."); int count = 0; // For counting number of connection while (true) { try { // Wait for client clientSocket = serverSocket.Accept(); count++; // Connection numbers // Get one, show some info _parent.Log("Connection number: " + count + ", Client accepted:" + clientSocket.RemoteEndPoint.ToString()); HTTPProcessor hp = new HTTPProcessor(clientSocket, _parent); // Test 500 request at a time, result: Error 0 % // Test 1000 request at a time, result: AvgError 10-30% | Sometimes error doesn't occur // Test 5000 request at a time, result: AvgError 30-50% Thread thread = new Thread(new ThreadStart(hp.Process)); // Assign hp.Process to run when thread start thread.Start(); // this will start hp.Process on thread } catch (Exception ex) { _parent.Log("Server starting error on connection number " + count + "\n"); _parent.Log(ex.Message + "\n" + ex.StackTrace); } } }
public TaskInfo(HTTPProcessor hp) { this.hp = hp; }
/// <summary> /// Server starting point /// </summary> public void Start() { while (true) { try { // Create listening socket, queue size is 5 now. IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, _port); serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); serverSocket.Bind(localEndPoint); serverSocket.Listen(5); _parent.Log("Server started at port " + _port + "."); break; } catch (Exception ex) { _parent.Log("Server started unsuccessfully."); _parent.Log(ex.Message); } _port = _port + 1; } while (true) { try { // Wait for client clientSocket = serverSocket.Accept(); // Get one, show some info _parent.Log("Client accepted:" + clientSocket.RemoteEndPoint.ToString()); HTTPProcessor hp = new HTTPProcessor(clientSocket, _parent); // Single thread //hp.Process(); // End single therad //Console.WriteLine("Thread"+Thread.CurrentThread.ThreadState); //multi thread //multithread start // Thread thread = new Thread(new ThreadStart(hp.Process)); //thread.Start(); /* * try * { * thread.Start(); * } * catch(ThreadAbortException) * { * Thread.ResetAbort(); * }*/ var max = Program.Configuration.GetSection("max_thread"); //get value max number threadpool from file configulation int t_max = Convert.ToInt32(max.Value); //convert maximum thread to number to get its value //Set threadpool size ThreadPool.SetMaxThreads(t_max, t_max); //set max number of worker and Asynchronous IO ThreadPool.QueueUserWorkItem(new WaitCallback(hp.Process)); //execute the method by Queue when the threadpool is in the available state } catch (Exception ex) { _parent.Log("Server starting error: " + ex.Message + "\n" + ex.StackTrace); } } }
private void foo(HTTPProcessor hp) { hp.Process(); }
public void ThreadProc(HTTPProcessor stateinfo) { stateinfo.Process(); Thread.Sleep(10); }