Thread: flush socket

  1. #1
    return 0;
    Join Date
    Jan 2005
    Location
    Netherlands
    Posts
    89

    flush socket

    What function would I use to flush a socket which was made with the socket() call? I've seen examples flushing sockets with fflush(), but those sockets were made using fdopen(). So is there a function that can flush sockets made with socket(), or is there a way to convert the value returned by socket() to an FILE* (so I can flush it with fflush)?

    Thanks in advance

  2. #2
    ~viaxd() viaxd's Avatar
    Join Date
    Aug 2003
    Posts
    246
    [edit] nevermind [/edit]
    :wq

  3. #3
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Well normal sockets aren't truely buffered. You maintain a buffer and use send() to send data. It returns with the number of characters sent. You then use that to index the array and send the rest of it, repeating as needed to finish sending your data.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    You cant force the data in your socket to be flushed like that. Depending on what you are trying to accomplish, you might want to look into setting the TCP_NODELAY flag. This will disable the Nagle TCP algorithm, which means that instead of waiting for the buffer to fill before flushing the packet, the data gets sent immediately. If you are writing network communication which needs quick responses (mouse movements in a game, or maybe even telnet or ssh), then it might be a good idea to try TCP_NODELAY.

  5. #5
    return 0;
    Join Date
    Jan 2005
    Location
    Netherlands
    Posts
    89
    Well my problem is that I'm writing a SOCKS proxy, and when using it with an FTP client it doesn't work correctly: my program sends the data it receives from the FTP server to the client, and when the FTP server closes the connection my proxy closes the connection with the client. Though the proxy has send the data before closing the connection, the client doesn't receive it. So I searched around the internet and found someone with the same problem. He got replies that his data was buffered and needed to be flushed, and he fixed it by using fflush() on his socket. But I can't use fflush() on my socket... So any ideas?

  6. #6
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Possibly, you need to call shutdown explicitly.
    MSDN: Graceful Shutdown, Linger Options, and Socket Closure

  7. #7
    return 0;
    Join Date
    Jan 2005
    Location
    Netherlands
    Posts
    89
    Ah thanks, it worked Apparently I should use shutdown(sock,SD_SEND) before closing the socket.

  8. #8
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    why can't you simply use closesocket() ?
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  9. #9
    Registered User
    Join Date
    Dec 2004
    Posts
    95
    Because that does a "hard close", and any data waiting in the winsock buffers to be sent won't be sent.

  10. #10
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    so it's better to do shutdown twice (one for recv. and one for send) ?
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  11. #11
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>Because that does a "hard close", and any data waiting in the winsock buffers to be sent won't be sent.
    I'm not so sure about that.. I found this on MSDN under closesocket():
    Quote Originally Posted by MSDN
    If SO_DONTLINGER is enabled (the default setting) it always returns immediately—connection is gracefully closed in the background.
    Is there something I'm missing here?
    Just Google It. √

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

  12. #12
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    I found this on the msdn:

    The shutdown function does not close the socket. Any resources attached to the socket will not be freed until closesocket is invoked.

    To assure that all data is sent and received on a connected socket before it is closed, an application should use shutdown to close connection before calling closesocket.
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  13. #13
    Registered User
    Join Date
    Dec 2004
    Posts
    95
    The MSDN Winsock docs are often a bit dodgy, sometimes contradicting each other.
    I think the basic point is that you should use shutdown() if you might have data to send pending.

  14. #14
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    but the real question, can it replace closesocket() ?
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  15. #15
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    no, it cannot. shutdown only stops the sending or receiving of data on a socket. It does not close it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. socket programming question, closing sockets...
    By ursula in forum Networking/Device Communication
    Replies: 2
    Last Post: 05-31-2009, 05:17 PM
  2. Socket Help - Multiple Clients
    By project95talon in forum C Programming
    Replies: 5
    Last Post: 11-17-2005, 02:51 AM
  3. when to close a socket
    By Wisefool in forum Networking/Device Communication
    Replies: 5
    Last Post: 11-02-2003, 10:33 AM
  4. problem closing a socket
    By Wisefool in forum C Programming
    Replies: 1
    Last Post: 10-28-2003, 01:38 PM
  5. socket newbie, losing a few chars from server to client
    By registering in forum Linux Programming
    Replies: 2
    Last Post: 06-07-2003, 11:48 AM