Coffeehouse Thread

4 posts

System.Net.Sockets.Socket bug?

Back to Forum: Coffeehouse
  • Ion Todirel

    Hey,

    i working on a FTP client using Sockets, and i have a problem in detecting if Socket is still conected, before i will send a command to server using Socket.Send method. This code is not working:

    // Conection with server ...... OK
    // Server response ............. 220
    ....

    // before sending a command (ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.NETDEVFX.v20.en/cpref10/html/P_System_Net_Sockets_Socket_Connected.htm)
    bool blockingState = clientSocket.Blocking;
    try
    {
          byte
    [] tmp = new byte[1];
         clientSocket.Blocking = false
    ;
         clientSocket.Send(tmp, 1, 0);
    }
    catch
    (SocketException)

          return
    (loggedIn = false);
    }
    finally
    {
         clientSocket.Blocking = blockingState;
    }

    then i try with this (detect connection timeout):
    if (clientSocket.Available > 0)
    {
         ReadResponse();
          if (response.Code == 426)
         {
             return (loggedIn
    = false);
         }
    }

    if i use and Receive method (after Send) all will be OK but why in MSDN docs its say that we can use Send or Receive? seems that Send is not working? it's may be a problem with server (not IIS)?.

    after all this code Socket.Connected is still true, even if i stopped server (after establishing connection with it, and send some commands).

  • Mike Dimmick

    Go and read the Winsock Programmer's FAQ. That'll answer a lot of questions about how to use sockets effectively.

    The only way the client can tell that the server has disconnected (if disconnected gracefully) is by reading all the data until the receive function returns 0 bytes. Until that point it doesn't see the FIN flag. The server doesn't set the FIN flag in a transmitted packet until it's sent all buffered data.

    In the case of an abnormal disconnection, eventually you'll either get a timeout, or you'll get an error response to sent data if the server sets the RST (Reset) flag in a response packet.

    The Connected state basically just means that you've successfully performed the three-way handshake to set up a connection (client sends SYN, server sends SYN-ACK, client sends ACK). Beyond that point it doesn't really have any significance.

  • Ion Todirel

    why Socket class does't support events? (OnClose, OnResonse...)

  • TommyCarlier

    Maybe because it can't detect it?

Comments closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums, or Contact Us and let us know.