protected override void OnStart(string[] args) { log4net.Info("/**************" + DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString() + "*****************/"); log4net.Info("Sevice is starting..."); RegisterLog("Sevice is starting...", "Info", "Service", "Unknown"); try { log4net.Info("Listen: " + ServerIP + ":" + PortNumber.ToString()); myServer = new XYNetServer(ServerIP, PortNumber, 0, 100); myServer.SetExceptionHandler(new ExceptionHandlerDelegate(this.ExceptionHandler)); myServer.SetStringInputHandler(new StringInputHandlerDelegate(this.StringInputHandler)); myServer.SetConnectionFilter(new ConnectionFilterDelegate(this.ConnectionFilter)); if (myServer.StartServer() == true) { log4net.Info("Server Listenning is Started"); RegisterLog("Server Listenning is Started", "Info", "Service", "Unknown"); } else { log4net.Info("Cannot Start Listen Server"); this.Stop(); } } catch (Exception oBug) { log4net.Error("Cannot Start Service" + oBug.Message); RegisterLog(oBug.Message, oBug.GetType().Name, oBug.Source, oBug.StackTrace); } }
static void Main(string[] args) { try { if (args.Length == 1) { myServer = new XYNetServer("", Convert.ToInt32(args[0]), 5, 10); myServer.SetConnectionFilter(new ConnectionFilterDelegate(Test.ConnectionFilter)); myServer.SetExceptionHandler(new ExceptionHandlerDelegate(Test.ExceptionHandler)); myServer.SetBinaryInputHandler(new BinaryInputHandlerDelegate(Test.BinaryInputHandler)); myServer.SetStringInputHandler(new StringInputHandlerDelegate(Test.StringInputHandler)); if (myServer.StartServer() == false) { throw myServer.GetLastException(); } System.Console.Out.WriteLine("Waiting for connections on Port {0}", args[0]); Thread.Sleep(600000); System.Console.Out.WriteLine("Thread count: " + myServer.GetThreadCount().ToString()); System.Console.Out.WriteLine("Client count: " + myServer.GetClientCount().ToString()); myServer.StopServer(); System.Console.Out.WriteLine("Done!"); } else if (args.Length == 2) { const int nSize = 100; const int nPause = 10; XYNetClient[] pClients = new XYNetClient[nSize]; for (int i = 0; i < nSize; i++) { pClients[i] = new XYNetClient(args[0], Convert.ToInt32(args[1])); if (pClients[i].Connect() == false) { throw pClients[i].GetLastException(); } Thread.Sleep(nPause); } for (int i = 0; i < nSize; i++) { if (pClients[i].SendStringData("Text sent as string data") == false) { throw pClients[i].GetLastException(); } Thread.Sleep(nPause); } for (int i = 0; i < nSize; i++) { Object[] pData = pClients[i].ReceiveData(); if (pData == null) { throw pClients[i].GetLastException(); } String sData = (String)(pData[1]); System.Console.Out.WriteLine("String data from server: " + sData); Thread.Sleep(nPause); } for (int i = 0; i < nSize; i++) { if (pClients[i].SendBinaryData(XYNetCommon.StringToBinary("Text sent as binary data")) == false) { throw pClients[i].GetLastException(); } Thread.Sleep(nPause); } for (int i = 0; i < nSize; i++) { Object[] pData = pClients[i].ReceiveData(); if (pData == null) { throw pClients[i].GetLastException(); } Byte[] pData2 = (Byte[])(pData[0]); System.Console.Out.WriteLine("Binary data from server: " + XYNetCommon.BinaryToString(pData2)); Thread.Sleep(nPause); } for (int i = 0; i < nSize; i++) { pClients[i].Reset(); } } else { throw new Exception("Invalid number of arguments"); } } catch (Exception oBug) { System.Console.Out.WriteLine("Error Type: " + oBug.GetType().Name); System.Console.Out.WriteLine("Error Message: " + oBug.Message); System.Console.Out.WriteLine("Error Source: " + oBug.Source); System.Console.Out.WriteLine("Error StackTrace: " + oBug.StackTrace); if (args.Length > 0) { System.Console.Out.WriteLine("arg0 = " + args[0]); } if (args.Length > 1) { System.Console.Out.WriteLine("arg1 = " + args[1]); } } }