V1/Cet.Develop.WP.IO/Net/SocketClient.cs
/// <summary>
/// Attempt a TCP socket connection to the given host over the given port
/// </summary>
/// <param name="hostName">The name of the host</param>
/// <param name="portNumber">The port number to connect</param>
/// <returns>True when the connection succeeded</returns>
Attempt a TCP socket connection to the given host over the given port
Attempt a TCP socket connection to the given host over the given port
public bool Connect(
            string hostName,
            int portNumber,
            int timeout = TIMEOUT_MILLISECONDS)
        {
            string result = "Operation Timeout";
            DnsEndPoint hostEntry = new DnsEndPoint(hostName, portNumber);
            _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            SocketAsyncEventArgs socketEventArg = new SocketAsyncEventArgs();
            socketEventArg.RemoteEndPoint = hostEntry;
            socketEventArg.Completed += new EventHandler<SocketAsyncEventArgs>(delegate(object s, SocketAsyncEventArgs e)
            {
                if (_socket.Connected)
                {
                    result = string.Empty;
                }
                else
                {
                    result = e.SocketError.ToString();
                }
                _clientDone.Set();
            });
            _clientDone.Reset();
            _socket.ConnectAsync(socketEventArg);
            _clientDone.WaitOne(timeout);
            this.LastError = result;
            return string.IsNullOrEmpty(result);
        }
public bool Connect ( string hostName , int portNumber , int timeout = TIMEOUT_MILLISECONDS ) { string result = " " ; DnsEndPoint hostEntry = new DnsEndPoint ( hostName , portNumber ) ; _socket = new Socket ( AddressFamily . InterNetwork , SocketType . Stream , ProtocolType . Tcp ) ; SocketAsyncEventArgs socketEventArg = new SocketAsyncEventArgs ( ) ; socketEventArg . RemoteEndPoint = hostEntry ; socketEventArg . Completed += new EventHandler < SocketAsyncEventArgs > ( delegate ( object s , SocketAsyncEventArgs e ) { if ( _socket . Connected ) { result = string . Empty ; } else { result = e . SocketError . ToString ( ) ; } _clientDone . Set ( ) ; } ) ; _clientDone . Reset ( ) ; _socket . ConnectAsync ( socketEventArg ) ; _clientDone . WaitOne ( timeout ) ; this . LastError = result ; return string . IsNullOrEmpty ( result ) ; }
Attempt a TCP socket connection to the given host over the given port
Attempt a TCP socket connection to the given host over the given port
// Create DnsEndPoint. The hostName and port are passed in to this method. // Create a stream-based, TCP socket using the InterNetwork Address Family.  // Create a SocketAsyncEventArgs object to be used in the connection request // Inline event handler for the Completed event. // Note: This event handler was implemented inline in order to make this method self-contained. //success // Retrieve the result of this request // Signal that the request is complete, unblocking the UI thread // Sets the state of the event to nonsignaled, causing threads to block // Make an asynchronous Connect request over the socket // Block the UI thread for a maximum of TIMEOUT_MILLISECONDS milliseconds. // If no response comes back within this time then proceed
True when the connection succeeded
True when the connection succeeded
hostName
string
The name of the host
The name of the host
portNumber
int
The port number to connect
The port number to connect
timeout
int