Esempio n. 1
0
 /// <summary>
 /// 开启TCP服务器
 /// </summary>
 /// <param name="ip">需要侦听的IP地址和端口</param>
 /// <returns></returns>
 public string StartServer(IPEndPoint ip)
 {
     if (Running)
     {
         return("侦听已经启动");
     }
     try
     {
         Socket sokserver = new Socket(ip.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
         sokserver.Bind(ip);
         sokserver.Listen(100);
         sokserver.BeginAccept(new AsyncCallback(AcceptCallBack), sokserver);
         SocketServer = sokserver;
         if (ShowListMsg != null)
         {
             ShowListMsg(null, string.Format("服务器【{0}】启动成功...", SocketServer.LocalEndPoint), DateTime.Now);
         }
         Running = true;
         return(SocketServer.LocalEndPoint.ToString());
     }
     catch (Exception ex)
     {
         if (ShowListMsg != null)
         {
             ShowListMsg(null, string.Format("服务器【{0}】启动失败", ip), DateTime.Now);
         }
         TmoShare.WriteTcpLog(string.Format("服务器【{0}】启动失败", ip), ex);
     }
     return(null);
 }
Esempio n. 2
0
 /// <summary>
 /// 开启TCP服务器
 /// </summary>
 /// <param name="ip">需要侦听的IP地址</param>
 /// <param name="port">需要侦听的端口号</param>
 /// <returns></returns>
 public string StartServer(string ip, int port)
 {
     try
     {
         IPEndPoint iep = new IPEndPoint(IPAddress.Parse(ip), port);
         return(StartServer(iep));
     }
     catch (Exception ex)
     {
         if (ShowListMsg != null)
         {
             ShowListMsg(null, string.Format("服务器【{0}:{1}】启动失败", ip, port), DateTime.Now);
         }
         TmoShare.WriteTcpLog(string.Format("服务器【{0}:{1}】启动失败", ip, port), ex);
     }
     return(null);
 }