Disconnecting the Server

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

To disconnect and shutdown a socket

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

    // shutdown the send half of the connection since no more data will be sent
    iResult = shutdown(ClientSocket, SD_SEND);
    if (iResult == SOCKET_ERROR) {
        printf("shutdown failed: %d\n", WSAGetLastError());
        closesocket(ClientSocket);
        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(ClientSocket);
    WSACleanup();
    
    return 0;
    

Complete Server Source Code

Getting Started With Winsock

Winsock Server Application

Receiving and Sending Data on the Server

Running the Winsock Client and Server Code Sample