closesocket and shutdown

This is a discussion on closesocket and shutdown within the Networking/Device Communication forums, part of the General Programming Boards category; Ok, this has been bothering me for a while. I read on MSDN that you should always call closesocket() after ...

  1. #1
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879

    Red face closesocket and shutdown

    Ok, this has been bothering me for a while. I read on MSDN that you should always call closesocket() after you create a socket using socket(). So I do it. But what I would like to know, is can the socket be re-opened, or does a new socket have to be created? Like, can I do this:
    Code:
    SOCKET s = socket(...);
    connect(s, ...);
    closesocket(s);
    
    connect(s, ...); //Is this ok?
    (...)
    
    //Or do I have to do this
    SOCKET s = socket(...);
    connect(s, ...);
    closesocket(s);
    
    SOCKET s2 = socket(...);
    connect(s2, ...);
    (...)
    If not, would it work if I used shutdown() instead?

    **EDIT**
    Also, am I right that closesocket() will cause the other end to get a recv() of 0 bytes?
    Last edited by Hunter2; 08-01-2003 at 09:01 PM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  2. #2
    Registered User johnnie2's Avatar
    Join Date
    Aug 2001
    Posts
    186
    Invoking closesocket() on a particular socket cancels any active connection and causes the operating system to discard internally-managed data regarding the socket. After the call, the unfortunate socket has effectively been wiped out of existence; Winsock has no memory of the socket and will insist that the handle does not describe a valid socket if used in further network calls. As a result, calling closesocket() renders the afflicted socket unusable.

    However, it is possible to sever an active connection without allowing the operating system to discard the socket entirely via DisconnectEx(). Unfortunately, DisconnectEx() is available under only Windows XP; you'll most likely be forced to use the second code sample to completely discard a socket and declare a new handle.
    Originally posted by Hunter2
    Also, am I right that closesocket() will cause the other end to get a recv() of 0 bytes?
    Yes; closesocket() will implicitly terminate an active connection, and the remote peer will receive zero bytes. Invoking shutdown() on a socket results in similar behavior.
    "Optimal decisions, once made, do not need to be changed." - Robert Sedgewick, Algorithms in C

  3. #3
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Heh, I actually posted this because of a bug I had where the server would "close" the socket but the client wouldn't know that it was closed, but I found out that it was just a bug in my socket wrapper class. Thanks anyways though, it's good to finally be sure about what closesocket() does
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. shutdown()
    By Hunter2 in forum Networking/Device Communication
    Replies: 6
    Last Post: 03-18-2004, 07:43 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21