Listening jack

I had a strange problem: I had never done this before, here is the server code (in this case, the firefox client), how I create it:

_Socket = new Socket( AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp );
_Socket.Bind( new IPEndPoint( Settings.IP, Settings.Port ) );
_Socket.Listen( 1000 );
_Socket.Blocking = false;

The way I accept the connection:

while( _IsWorking )
{
    if( listener.Socket.Poll( -1, SelectMode.SelectRead ) )
    {
        Socket clientSocket = listener.Socket.Accept();
        clientSocket.Blocking = false;
        clientSocket.SetSocketOption( SocketOptionLevel.Tcp, SocketOptionName.NoDelay, true );
    }
}

So, I expect it to hang on listener.Socket.Poll until a new connection appears, but after the first start it hangs in the poll forever. I tried to interrogate it constantly with a shorter delay, say 10 microseconds, then it never goes to SelectMode.SelectRead. I think this may be due to the reuse of the client socket? Perhaps I am not closing the client socket and the client (firefox) decides to use the old socket?

I will disconnect the client socket as follows:

Context.Socket.Shutdown( SocketShutdown.Both ); // context is just a wrapper around socket
Context.Socket.Close();

What can cause this problem?

+3
source share
1

, ? TCPListener, Socket.

, , :

listener.Socket.Poll( -1, SelectMode.SelectRead ) )

Sockets.TCPListener.Pending(), .NET, , , :

public bool Pending()
{
    if (!this.m_Active)
    {
        throw new InvalidOperationException(SR.GetString("net_stopped"));
    }
    return this.m_ServerSocket.Poll(0, SelectMode.SelectRead);
}

, MSDN TCPListener.Pending() , , 100 %?

+1

Source: https://habr.com/ru/post/1744103/


All Articles