Exemplo n.º 1
0
 public virtual bool Connect()
 {
     try
     {
         Monitor.Enter(this);
         XyNetCommon.SetSocketPermission();
         Reset();
         _mSocketClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
         IPEndPoint myEnd = null;
         try
         {
             myEnd = (_mSRemoteAddress == "") ? (new IPEndPoint(Dns.GetHostByName(Dns.GetHostName()).AddressList[0], _mNRemotePort)) : (new IPEndPoint(IPAddress.Parse(_mSRemoteAddress), _mNRemotePort));
         }
         catch (Exception) { }
         if (myEnd == null)
         {
             myEnd = new IPEndPoint(Dns.GetHostByName(_mSRemoteAddress).AddressList[0], _mNRemotePort);
         }
         _mSocketClient.Connect(myEnd);
         return(true);
     }
     catch (Exception oBug)
     {
         _mException = oBug;
         try
         {
             _mSocketClient.Shutdown(SocketShutdown.Both);
             _mSocketClient.Close();
         }
         catch (Exception) { }
         return(false);
     }
     finally { Monitor.Exit(this); }
 }
Exemplo n.º 2
0
 public bool StartServer()
 {
     try
     {
         Monitor.Enter(this);
         XyNetCommon.SetSocketPermission();
         StopServer();
         _mThreadPool.SetThreadErrorHandler(ThreadErrorHandler);
         _mThreadPool.StartThreadPool(_mNMinThreadCount, _mNMaxThreadCount);
         _mSocketServer = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
         _mSocketServer.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1);
         var myEnd = (_mSAddress == "") ? (new IPEndPoint(Dns.GetHostByName(Dns.GetHostName()).AddressList[0], _mNPort)) : (new IPEndPoint(IPAddress.Parse(_mSAddress), _mNPort));
         _mSocketServer.Bind(myEnd);
         _mSocketServer.Listen(MnListenBacklog);
         _mThreadPool.InsertWorkItem("Accept Clients", new AcceptClientsDelegate(AcceptClients), null, false);
         _mThreadPool.InsertWorkItem("Detect Input", new DetectInputDelegate(DetectInput), null, false);
         return(true);
     }
     catch (Exception oBug)
     {
         _mException = oBug;
         return(false);
     }
     finally { Monitor.Exit(this); }
 }