Results 1 to 9 of 9

Thread: Network Disconnections

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2002
    Posts
    12

    Network Disconnections

    I am working on a server, and when someone connects to my server I assign them an ID number. When they exit my server by typing QUIT, the ID number is released so that someone else can use the ID number.

    My problem is... what if someone is disconnected from my server, like let's say their computer crashes and they disconnect without typing a QUIT command or their internet connection gets cut off? How can I make it so that when my program releases the socket, it also does other things like release the ID number, and print it to a log file, or carry out the same procedures that it would carry out if the user types QUIT to exit instead of disconnecting?

    I am programming in MSVC++ .NET using the winsock2 library, so sends are carried out by send() and receives are carried out by recv() and so on...

  2. #2
    Frenzied Member Technocrat's Avatar
    Join Date
    Jan 2000
    Location
    I live in the 1s and 0s of everyones data streams
    Posts
    1,024
    I can't give you any real specifics bacause I havent seen an of your program, but I can give you some ideas.

    I would think you could have a timer that checks your sockets for inactivity (idle). After a certian idle time has been reached, send your program the disconnect connect command for that socket. Then fire your log function.
    MSVS 6, .NET & .NET 2003 Pro
    I HATE MSDN with .NET & .NET 2003!!!

    Check out my sites:
    http://www.filthyhands.com
    http://www.techno-coding.com


  3. #3

    Thread Starter
    New Member
    Join Date
    Sep 2002
    Posts
    12
    This sounds like a good general idea and I think I can make a thread that specifically checks for disconnections every 5-10 minutes.

    Is this how most networks would handle something like this? I mean is there maybe something I'm missing that's part of the winsock2 library?

    Otherwise I'll get to work on the idea you mentioned above.

  4. #4
    Frenzied Member Technocrat's Avatar
    Join Date
    Jan 2000
    Location
    I live in the 1s and 0s of everyones data streams
    Posts
    1,024
    After think about it a bit more, I think I remember a better way to do it that is part of Winsock. Are you using pure API or are you using MFC? I know how to do it in API, but don't know how to do it in MFC.

    In API you can bind the disconnect command to a Window message. First define a Message:

    PHP Code:
    #define SOCKET_DISCONNECT    WM_USER + 1 
    Just use this some where in a startup:

    PHP Code:
    if(WSAAsyncSelect(sckNameWindow_hWndSOCKET_DISCONNECTFD_CLOSE) == SOCKET_ERROR)
        {
            
    ErrorHandle;
        } 
    Then in your message handler:

    PHP Code:
    case SOCKET_DISCONNECT:
      
    DoWhatYouWanted;
      
    Log;
      break; 
    This is its own message, its not a sub message.

    This will work for about 80% of disconnects like the way you are talking. There are those few that happen when a program they are using crashes and Windows does not release the socket or connection. In order to catch those you will have to watch for idles.

    If you are using MFC, I dont know how to do it. Maybe with some research I can find it.
    MSVS 6, .NET & .NET 2003 Pro
    I HATE MSDN with .NET & .NET 2003!!!

    Check out my sites:
    http://www.filthyhands.com
    http://www.techno-coding.com


  5. #5

    Thread Starter
    New Member
    Join Date
    Sep 2002
    Posts
    12

    MFC... heh

    That sounds like an even easier way to handle disconnections, unfortunately though, I am using MFC, not API so it wouldn't work.

    If you can find out, or maybe point me in the right direction as to where I can find out, I'd like to know.

    Thanks for responding.

  6. #6
    Frenzied Member Technocrat's Avatar
    Join Date
    Jan 2000
    Location
    I live in the 1s and 0s of everyones data streams
    Posts
    1,024
    Sorry I am a bit rusty with MFC, but it looks like all you need to do, it override the OnClose with your socket. Then do what needs to be done, then return control back to windows to close it.
    MSVS 6, .NET & .NET 2003 Pro
    I HATE MSDN with .NET & .NET 2003!!!

    Check out my sites:
    http://www.filthyhands.com
    http://www.techno-coding.com


  7. #7

    Thread Starter
    New Member
    Join Date
    Sep 2002
    Posts
    12
    That's alright.

    What you said at the end suggests that even with a routine that acts before the disconnect, it only works 80 percent of the time so the only real way of preventing people from disconnecting without losing their data is to check for idles.

  8. #8
    Frenzied Member Technocrat's Avatar
    Join Date
    Jan 2000
    Location
    I live in the 1s and 0s of everyones data streams
    Posts
    1,024
    Still that's 80%
    I think that's what you should work on.
    If I get some more time maybe I can find a good example some where.
    MSVS 6, .NET & .NET 2003 Pro
    I HATE MSDN with .NET & .NET 2003!!!

    Check out my sites:
    http://www.filthyhands.com
    http://www.techno-coding.com


  9. #9
    Frenzied Member Technocrat's Avatar
    Join Date
    Jan 2000
    Location
    I live in the 1s and 0s of everyones data streams
    Posts
    1,024
    Code guru has some good examples, and most of their stuff is MFC. Look at the bottom of this page:

    http://www.codeguru.com/network/index.shtml
    MSVS 6, .NET & .NET 2003 Pro
    I HATE MSDN with .NET & .NET 2003!!!

    Check out my sites:
    http://www.filthyhands.com
    http://www.techno-coding.com


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