TcpConnection_Lib/TcpConnection_Lib.cs
/// <summary>
/// Try connecting a client to a specific endpoint.
/// </summary>
/// <param name="ipAddress"></param>
/// <param name="port"></param>
/// <returns>True, if a connection could be accomplished.</returns>
Try connecting a client to a specific endpoint.
Try connecting a client to a specific endpoint .
public bool TryConnect(string ipAddress, int port)
        {
            lock (_syncRoot)
            {
                try
                {
                    if (TcpIsConnected || _client != null)
                    {
                        return false;
                    }
                    _client = new TcpClient();
                    _client.Connect(ipAddress, port);
                    _client.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
                    return TcpIsConnected = _client.Connected;
                }
                catch (SocketException)
                {
                    return false;
                }
                catch (Exception Ex)
                {
                    throw Ex;
                }
            }
        }
public bool TryConnect ( string ipAddress , int port ) { lock ( _syncRoot ) { try { if ( TcpIsConnected || _client != null ) { return false ; } _client = new TcpClient ( ) ; _client . Connect ( ipAddress , port ) ; _client . Client . SetSocketOption ( SocketOptionLevel . Socket , SocketOptionName . KeepAlive , true ) ; return TcpIsConnected = _client . Connected ; } catch ( SocketException ) { return false ; } catch ( Exception Ex ) { throw Ex ; } } }
Try connecting a client to a specific endpoint.
Try connecting a client to a specific endpoint .
True, if a connection could be accomplished.
True if a connection could be accomplished .
ipAddress
string
None
port
int
None