Disconnecting the Client

Once the client is completed sending and receiving data, the client disconnects from the server and shutdowns the socket.

To disconnect and shutdown a socket

  1. When the client is done sending data to the server, the shutdown function can be called specifying SD_SEND to shutdown the sending side of the socket. This allows the server to release some of the resources for this socket. The client application can still receive data on the socket.

    // shutdown the send half of the connection since no more data will be sent
    iResult = shutdown(ConnectSocket, SD_SEND);
    if (iResult == SOCKET_ERROR) {
        printf("shutdown failed: %d\n", WSAGetLastError());
        closesocket(ConnectSocket);
        WSACleanup();
        return 1;
    }
    
  2. When the client application is done receiving data, the closesocket function is called to close the socket.

    When the client application is completed using the Windows Sockets DLL, the WSACleanup function is called to release resources.

    // cleanup
    closesocket(ConnectSocket);
    WSACleanup();
    
    return 0;
    

Complete Client Source Code

Getting Started With Winsock

Winsock Client Application

Sending and Receiving Data on the Client