Thread: Client closes before server receives

  1. #1
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001

    Client closes before server receives

    Okay, I have a client (using plain old winsock) connecting to a server (using CAsyncSocket). It first sends a message saying some data is coming, then it sends the data, then it closesocket()'s the connection. What happens serverside is it receives the "data on the way" message in OnReceive() then on the next OnReceive() (for the transfer of the actual data), Receive() returns SOCKET_ERROR with a GetLastError() of WSAENOTSOCK. So I finally figured out that the reason this happens is that the client's closure of the socket after the send apparently screws the server out of getting the data. Is this what's actually happening?

    I suppose a simple way around it is to just always make sure the server has the last word by sending an OK_TO_QUIT message to the client before he closes, but is there another way around it?

    Your thoughts are appreciated.

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    There is a very small chance this may help:

    I'd check your server application first.

  3. #3
    Registered User
    Join Date
    Nov 2001

    One solution is to wait for the server to send a final message to confirm that it has received all packets.


  4. #4
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    What about shutdown()? I believe when you call shutdown() the server receives the 'socket closed' byte once all data after the shutdown() has been received, even though on the client side the socket isn't really officially closed. Once the server calls closesocket(), the client receives the 'socket closed' byte as well, and then you know that it's safe to close the socket. For example:
    send(sock, "Data coming.", strlen("Data coming."), 0);
    send(sock, "This is the data.", strlen("This is the data."), 0);
    shutdown(sock, SD_SEND);
    char temp;
    recv(sock, &temp, 1);
    Of course, you should probably do the whole error checking thing and all, but if the server isn't supposed to send any data back to the client, that should work.
    Last edited by Hunter2; 07-06-2004 at 04:10 PM.
    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. server client application - (i really need your help)
    By sarahnetworking in forum C Programming
    Replies: 3
    Last Post: 03-01-2008, 10:54 PM
  2. c program client server
    By steve1_rm in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-24-2008, 10:33 AM
  3. Where's the EPIPE signal?
    By marc.andrysco in forum Networking/Device Communication
    Replies: 0
    Last Post: 12-23-2006, 08:04 PM
  4. Server and Client process
    By wise_ron in forum Networking/Device Communication
    Replies: 1
    Last Post: 10-07-2006, 01:11 AM
  5. Unicode vurses Non Unicode client server application with winsock2 query?
    By dp_76 in forum Networking/Device Communication
    Replies: 0
    Last Post: 05-16-2005, 07:26 AM