public static void Main(String[] args) { try { Int32 port = GetValue(args, 0, DEFAULT_PORT); Int32 numConnections = GetValue(args, 1, DEFAULT_NUM_CONNECTIONS); Int32 bufferSize = GetValue(args, 2, DEFAULT_BUFFER_SIZE); SocketListener sl = new SocketListener(numConnections, bufferSize); sl.Start(port); Console.WriteLine("Server listening on port {0}. Press any key to terminate the server process...", port); Console.Read(); sl.Stop(); } catch (IndexOutOfRangeException) { PrintUsage(); } catch (FormatException) { PrintUsage(); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } }
static void Main(String[] args) { try { SocketListener listener = new SocketListener(DEFAULT_MAX_NUM_CONNECTIONS, DEFAULT_BUFFER_SIZE); listener.Start(new IPEndPoint(IPAddress.Parse("127.0.0.1"), DEFAULT_PORT)); Console.WriteLine("Server listening on port {0}. Press any key to terminate the server process...", DEFAULT_PORT); Console.Read(); listener.Stop(); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } }
private void StartAsyncTcpService() { Log.Write("开始启动异步TCP服务:"); IPAddress ipd = IPAddress.Any; if (Configs.Address.ToUpper() != "ANY") ipd = IPAddress.Parse(Configs.Address); IPEndPoint localEndPoint = new IPEndPoint(ipd, Configs.Port); //This object holds a lot of settings that we pass from Main method //to the SocketListener. In a real app, you might want to read //these settings from a database or windows registry settings that //you would create. SocketListenerSettings theSocketListenerSettings = new SocketListenerSettings (Configs.MaxNumberOfConnections, Configs.ExcessSaeaObjectsInPool, Configs.Backlog, Configs.MaxSimultaneousAcceptOps , Configs.ReceivePrefixLength, Configs.ReceiveBufferSize, Configs.sendPrefixLength, Configs.opsToPreAlloc, localEndPoint); //instantiate the SocketListener. SocketListener sl = new SocketListener(theSocketListenerSettings); sl.DataReceived += Socket_DataReceived; Log.Write("异步TCP服务启动成功:"); }
//____________________________________________________________________________ static void ManageClosing(SocketListener socketListener) { string stringToCompare = ""; string theEntry = ""; while (stringToCompare != closeString) { if (watchProgramFlow == true) { Console.WriteLine(wpfTrueString); } else { Console.WriteLine(wpfFalseString); } theEntry = Console.ReadLine().ToUpper(); switch (theEntry) { case checkString: Console.WriteLine("Number of current accepted connections = " + socketListener.numberOfAcceptedSockets); break; case wpf: if (Program.watchProgramFlow == false) { Program.watchProgramFlow = true; Console.WriteLine("Changed watchProgramFlow to true."); Program.testWriter.WriteLine("\r\nStart logging program flow.\r\n"); } else { Console.WriteLine("Program flow was already being logged."); } break; case wpfNo: if (Program.watchProgramFlow == true) { Program.watchProgramFlow = false; Console.WriteLine("Changed watchProgramFlow to false."); Program.testWriter.WriteLine("\r\nStopped logging program flow.\r\n"); } else { Console.WriteLine("Program flow was already NOT being logged."); } break; case closeString: stringToCompare = closeString; break; default: Console.WriteLine("Unrecognized entry"); break; } } WriteData(); }
static void ManageClosing(SocketListener socketListener) { string stringToCompare = ""; string theEntry = ""; while (stringToCompare != closeString) { if (watchProgramFlow == true) { Console.WriteLine(wpfTrueString); } else { Console.WriteLine(wpfFalseString); } theEntry = Console.ReadLine().ToUpper(); switch (theEntry) { case checkString: Console.WriteLine("Number of current accepted connections = " + socketListener.NumberOfAcceptedSockets); break; case wpf: if (Program.watchProgramFlow == false) { Program.watchProgramFlow = true; Console.WriteLine("Changed watchProgramFlow to true."); Program.testWriter.Log("\r\nStart logging program flow.\r\n"); } else { Console.WriteLine("Program flow was already being logged."); } break; case wpfNo: if (Program.watchProgramFlow == true) { Program.watchProgramFlow = false; Console.WriteLine("Changed watchProgramFlow to false."); Program.testWriter.Log("\r\nStopped logging program flow.\r\n"); } else { Console.WriteLine("Program flow was already NOT being logged."); } break; case closeString: stringToCompare = closeString; break; default: Console.WriteLine("Unrecognized entry"); break; } } WriteData(); }
// public static ILogger Logger = new TestFileWriter(); static void Main(String[] args) { // Just used to calculate # of received transmissions at the end. startingTid = mainTransMissionId; // Before the app starts, let's build strings for you to use the console. BuildStringsForServerConsole(); // Create List<T> to hold data, unless we are running a long test, which // would create too much data to store in a list. if (runLongTest == false) { listOfDataHolders = new List<DataHolder>(); } //Create a log file writer, so you can see the flow easily. //It can be printed. Makes it easier to figure out complex program flow. //The log StreamWriter uses a buffer. So it will only work right if you close //the server console properly at the end of the test. testWriter = new TestFileWriter(); try { // Get endpoint for the listener. var localEndPoint = new IPEndPoint(IPAddress.Any, port); WriteInfoToConsole(localEndPoint); //This object holds a lot of settings that we pass from Main method //to the SocketListener. In a real app, you might want to read //these settings from a database or windows registry settings that //you would create. var theSocketListenerSettings = new SocketListenerSettings (maxNumberOfConnections, excessSaeaObjectsInPool, backlog, maxSimultaneousAcceptOps, receivePrefixLength, testBufferSize, sendPrefixLength, opsToPreAlloc, localEndPoint, true, true); //instantiate the SocketListener. var socketListener = new SocketListener(theSocketListenerSettings, testWriter); socketListener.Init(); socketListener.StartListen(); ManageClosing(socketListener); } catch (Exception ex) { Console.WriteLine("Error: " + ex.Message); } finally { // close the stream for test file writing try { testWriter.Close(); } catch { Console.WriteLine("Could not close log properly."); } } }