public void ThreadCallBack(Object obj) { _clinet = obj as ThreadTcpClinet; _networkstream = _clinet.TcpClinet.GetStream(); _formatter = new BinaryFormatter(); bool running = true; TSqlAdapter SqlAdapter = new TSqlAdapter(); try { while (running) { _package = (TPackage)_formatter.Deserialize( _networkstream); if (_package.MagicId == 0) { running = false; Toos.Msg_Warn("客户端{0:D5}[{1}]请求结束会话\n", _clinet.ThreadId, _clinet.TcpClinet.Client.RemoteEndPoint.ToString()); break; } SqlAdapter.Save(_package); Toos.Msg_Message("{0}=>{1}#{2};{3};{4}\n", _clinet.TcpClinet.Client.RemoteEndPoint.ToString(), Toos.GetLocalIP(), _package.NickFrom, _package.NickTo, _package.Text); } } catch (Exception ex) { Toos.Msg_Error("错误:{0}\n", ex.StackTrace); } finally { Toos.Msg_Warn("线程{0:D5},远程{1} 结束 [OK] .\n", _clinet.ThreadId, _clinet.TcpClinet.Client.RemoteEndPoint.ToString()); _networkstream.Close(); } }
public static void Main(string[] args) { int _port = 8888; string _serverip; int minWorkerThreads, maxWorkerThreads; int minCompletionThreads, maxCompletionThreads; System.Threading.ThreadPool.GetMinThreads(out minWorkerThreads, out minCompletionThreads); // Get the maximum number of completion threads System.Threading.ThreadPool.GetMaxThreads(out maxWorkerThreads, out maxCompletionThreads); // Set the new max worker threads, but keep the old max completion threads //ThreadPool.SetMaxThreads(someDifferentValue, maxCompletionThreads); Toos.Msg_Message("本服务器默认提供最小线程数:{0:D5};最大线程数:{1:D5}\n", minWorkerThreads, maxWorkerThreads); if (args.Length > 0) { Toos.Msg_Message("*本服务器IP:{0}\n", args[0]); _serverip = args[0]; } else { Toos.Msg_Message("本服务器IP:{0}\n", Toos.GetLocalIP()); Toos.Msg_Message("输入IP地址或者直接回车使用默认地址:"); _serverip = Console.ReadLine(); if (_serverip == "") { _serverip = Toos.GetLocalIP(); } } TServer server = new TServer(_serverip, _port); server.Start(); }
public static void Main(string[] args) { string ipString; int port = 8888; TPackage _package; IFormatter _formater = new BinaryFormatter(); NetworkStream _networkstream; Random rand = new Random(); IPAddress ipaddress = new IPAddress(0); if (args.Length > 0 && IPAddress.TryParse(args[0], out ipaddress)) { ipString = args[0]; Toos.Msg_Message("远程服务器地址:{0}", ipString); } else { Toos.Msg_Message("请输远程服务器地址:"); ipString = Console.ReadLine(); } TcpClient clinet = new TcpClient(); clinet.Connect(new IPEndPoint(IPAddress.Parse(ipString), port)); Toos.Msg_Message("发送超时:{0:D};接受超时{1:D}", clinet.SendTimeout, clinet.ReceiveTimeout); _networkstream = clinet.GetStream(); Toos.Msg_Error("正在连接{0}:{1} ...\n", ipString, port); Toos.Msg_Message("程序运行中按任意键退出:D\n"); while (!Console.KeyAvailable) { _package = new TPackage() { NickFrom = "发送:" + rand.Next(), NickTo = "接收:" + rand.Next(), Text = "数据:" + rand.Next(), QQTo = rand.Next().ToString(), QQFrom = rand.Next().ToString(), DTime = DateTime.Now, MagicId = 1 }; System.Threading.Thread.Sleep(500); try { _formater.Serialize(_networkstream, _package); Toos.Msg_Message("{0}=>{1}#{2};{3};{4}\n", Toos.GetLocalIP(), ipString, _package.NickFrom, _package.NickTo, _package.Text); } catch (Exception ex) { Toos.Msg_Error("错误:{0}\n", ex.Message); } } //发送魔术包! _package = new TPackage() { NickFrom = "准备" + rand.Next(), NickTo = "结束" + rand.Next(), Text = "通讯" + rand.Next(), DTime = DateTime.Now, MagicId = 0 }; _formater.Serialize(_networkstream, _package); _networkstream.Close(); clinet.Close(); }