CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2009
    Posts
    252

    Winsock Connection Drop Detection

    Hello, im using a SocketWrapper Named "SocketCom" it detects the connection drop fine but sometimes it doesnt... i have no idea why the socket thinks is online while is not.


    this is the code which detects the connection drop
    Code:
    LPBYTE  lpData  = (LPBYTE)&stMsgProxy;
    
        while( IsOpen() ){ // if the port is Open
            // Blocking mode: Wait for event
            dwBytes = ReadComm(lpData, dwSize, dwTimeout);
    
            // Error? - need to signal error
            if (dwBytes == (DWORD)-1L){
                // Do not send event if we are closing
                if (IsOpen()){
                    if ( bSmartAddressing ){
                        RemoveFromList( stMsgProxy.address );
                    }
                    OnEvent( EVT_CONDROP, &stMsgProxy.address ); // lost connection
                }
    As i understand it attemps to read data from the Socket and if the read bytes result in
    (DWORD)-1L launches the EVT_CONDROP.

    I have another doub what does the L means there?

    and why would this fail to detect the connection drop while the Run Thread still running.

    and why reading that would give the signal to detect connection drop.

    Thx in advance.

  2. #2
    Join Date
    Mar 2001
    Posts
    2,529

    Re: Winsock Connection Drop Detection

    I believe L is part of a type cast.

    If you use Winsock, WSAGetLastError() would return socket errors. The library you are using will have its own error checking implemented. Look in the Documentation.

    http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx
    ahoodin
    To keep the plot moving, that's why.

  3. #3
    Join Date
    Feb 2009
    Posts
    252

    Re: Winsock Connection Drop Detection

    Quote Originally Posted by ahoodin View Post
    I believe L is part of a type cast.

    If you use Winsock, WSAGetLastError() would return socket errors. The library you are using will have its own error checking implemented. Look in the Documentation.

    http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx
    thx for ur answer yea i read the documentation of recv and it says when the socket closes "gracefully" it will return 0 or the error values that i suposse they are negative values.


    but i found that the sockets dont necesary detect Cable pull or anyother abnormal termination like crashes etc.

    my program needs to handle these too but i think i fond the solution in this article.


    http://tangentsoft.net/wskfaq/newbie.html
    2.14 - How do I detect an abnormal network disconnect?

    THX for ur time.

  4. #4
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Winsock Connection Drop Detection

    No L is an integer suffix that tell the compiler that the 1 should be interpreted as a long, not an integer (default for a plain number). See this http://msdn.microsoft.com/en-us/library/00a1awxf.aspx

    For 32-bit MSVC the L isn't needed since an int is 32 bits and so is a DWORD (back in the 16-bit era the L was needed so the snippet might be from an old source)
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  5. #5
    Join Date
    Mar 2001
    Posts
    2,529

    Re: Winsock Connection Drop Detection

    Quote Originally Posted by Alphadan View Post
    thx for ur answer yea i read the documentation of recv and it says when the socket closes "gracefully" it will return 0 or the error values that i suposse they are negative values.
    Well you have to see what these error values are. There should be a table or list somewhere. Otherwise you can't possibly know.
    ahoodin
    To keep the plot moving, that's why.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured