Exemplo n.º 1
0
 void TCPServerControl()
 {
     while (true)
     {
         try
         {
             try
             {
                 System.Net.IPHostEntry oIPHost = System.Net.Dns.GetHostByName(Environment.MachineName);
                 if (oIPHost.AddressList.Length > 0)
                 {
                     string IPAddress = oIPHost.AddressList[0].ToString();
                     Socket client    = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                     client.Connect(IPAddress, int.Parse(MainStatic.Port));
                     client.Close();
                     ToolAPI.XMLOperation.WriteLogXmlNoTail("测试TCP监听正常", MainStatic.Port);
                 }
             }
             catch (Exception ex)
             {
                 ToolAPI.XMLOperation.WriteLogXmlNoTail("测试TCP监听异常", ex.Message);
                 try
                 {
                     TCPOperation.CloseListener();
                     TCPOperation = null;
                 }
                 catch (Exception ee)
                 {
                     ToolAPI.XMLOperation.WriteLogXmlNoTail("测试TCP监听服务停止异常", ee.Message);
                 }
                 try
                 {
                     InitTcpSocketServer();
                     ToolAPI.XMLOperation.WriteLogXmlNoTail("测试TCP监听再次被启动", "");
                 }
                 catch (Exception ef)
                 {
                     ToolAPI.XMLOperation.WriteLogXmlNoTail("测试TCP监听再次启动异常", ef.Message);
                 }
             }
         }
         catch (Exception et)
         {
             ToolAPI.XMLOperation.WriteLogXmlNoTail("测试TCP监听线程异常", et.Message);
         }
         Thread.Sleep(30000);
     }
 }
Exemplo n.º 2
0
 /// <summary>
 /// 初始化Socket服务
 /// </summary>
 /// <param name="tcp"></param>
 void InitTcpSocketServer()
 {
     TCPOperation          = new TCPOperation(Subject);
     TCPOperation.External = new ExternalClass();
     TCPOperation.OpenListener(int.Parse(MainStatic.Port), 10000);
 }