TcpConnection_Lib/TcpConnection_Lib.cs
/// <summary>
/// Try to listen for clients on a specific port and any available IP address.
/// </summary>
/// <param name="port"></param>
/// <returns>True, if a connection could be accomplished.</returns>
Try to listen for clients on a specific port and any available IP address.
Try to listen for clients on a specific port and any available IP address .
public bool TryListen(int port)
        {
            lock (_syncRoot)
            {
                try
                {
                    if (TcpIsConnected || _listener != null)
                    {
                        return false;
                    }
                    IPEndPoint ipLocalEndPoint = new IPEndPoint(IPAddress.Any, port);
                    _listener = new TcpListener(ipLocalEndPoint);
                    _listener.Start(port);
                    _client = _listener.AcceptTcpClient();
                    _client.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
                    _listener?.Stop();
                    return TcpIsConnected = _client.Connected;
                }
                catch (SocketException)
                {
                    return false;
                }
                catch (Exception Ex)
                {
                    throw Ex;
                }
            }
        }
public bool TryListen ( int port ) { lock ( _syncRoot ) { try { if ( TcpIsConnected || _listener != null ) { return false ; } IPEndPoint ipLocalEndPoint = new IPEndPoint ( IPAddress . Any , port ) ; _listener = new TcpListener ( ipLocalEndPoint ) ; _listener . Start ( port ) ; _client = _listener . AcceptTcpClient ( ) ; _client . Client . SetSocketOption ( SocketOptionLevel . Socket , SocketOptionName . KeepAlive , true ) ; _listener ? . Stop ( ) ; return TcpIsConnected = _client . Connected ; } catch ( SocketException ) { return false ; } catch ( Exception Ex ) { throw Ex ; } } }
Try to listen for clients on a specific port and any available IP address.
Try to listen for clients on a specific port and any available IP address .
True, if a connection could be accomplished.
True if a connection could be accomplished .
port
int
None