Пример #1
0
 /// <summary>
 /// Attempt to connect to a server at the specified address.
 /// Base method calls Socket's Connect(str, int) method using the configured port number and invokes OnNewConnection()
 /// </summary>
 /// <param name="host">DNS hostname to connect to or IPv4/IPv6 address string</param>
 public virtual void Connect(string host)
 {
     try
     {
         this.Socket.Connect(host, this.Port);
         if (this.Socket.RemoteEndPoint != null)
         {
             if (this.Socket.RemoteEndPoint is IPEndPoint remoteEndpoint)
             {
                 Server = new NetworkNode(remoteEndpoint.Address, this.Protocol, (ushort)remoteEndpoint.Port, this.Socket);
                 OnNewConnection(Server);
             }
         }
     }
     catch (SocketException se)
     {
         if (SocketExceptionOccured != null)
         {
             OnSocketException(Server, se);
         }
         else
         {
             throw; // Rethrow, preserving stack details
         }
     }
 }
Пример #2
0
        public void ListenForConnections()
        {
            while (IsRunning)
            {
                try
                {
                    Socket newSocket = this.Socket.Accept();
                    if (newSocket != null)
                    {
                        if (newSocket.RemoteEndPoint is IPEndPoint remoteEndpoint)
                        {
                            NetworkNode Remote = new NetworkNode(remoteEndpoint.Address, this.Protocol, (ushort)remoteEndpoint.Port, newSocket);

                            this.OnNewConnection(Remote);
                            _ = ListenForMessagesAsync(Remote);
                        }
                    }
                }
                catch (SocketException)
                {
                    IsRunning = false;
                }
            }
        }