HTTP请求C ++函数并不总是提交请求(很少)

I have an HTTP post_data function in C++ used for sending an HTTP request to my web server.

About 1 in every 50 attempts, the request actually goes through, the other times, it doesn't get sent.

I have no idea why but it's supposed to go through 100% of the times. Here's my post_data function:

One of the reasons why I'm not using an HTTP library is because I just couldn't get them working a few months ago.

std::string MasterServerHandler::post_data(std::string file, std::string data){
    string request;
    string response;
    int resp_leng;

    char buffer[BUFFERSIZE];
    struct sockaddr_in serveraddr;
    int sock;

    WSADATA wsaData;
    const char* ipaddress  = cst::masterIP.c_str();
    std::string host = cst::masterHost;

    int port = 80;

    request+="GET /folder/" + string(file) + data + " HTTP/1.0
";
    request+="Host: " + host + "
";
    request+="
";
     //open socket


    if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
        cout << "WEB socket() failed" << endl;

    //connect
    memset(&serveraddr, 0, sizeof(serveraddr));
    serveraddr.sin_family      = AF_INET;
    serveraddr.sin_addr.s_addr = inet_addr(ipaddress);
    serveraddr.sin_port        = htons((unsigned short) port);
    if (connect(sock, (struct sockaddr *) &serveraddr, sizeof(serveraddr)) < 0)
        cout << "WEB connect() failed" << endl;

    //send request
    if (send(sock, request.c_str(), request.length(), 0) != request.length())
        cout << "WEB send() sent a different number of bytes than expected" << endl;;

    //If we care about the response
        //get response
        response = "";
        resp_leng= BUFFERSIZE;
        while (resp_leng == BUFFERSIZE)
        {
            resp_leng= recv(sock, (char*)&buffer, BUFFERSIZE, 0);
            if (resp_leng>0)
                response+= string(buffer).substr(0,resp_leng);
            //note: download lag is not handled in this code
        }

   //disconnect
   closesocket(sock);

   return response;

}

So does anyone know why sometimes this HTTP call doesn't go through all the way? I took away closesocket() and it works but, if I take that away then there will be packet leaks and that's bad because this function gets called every so often.

I also tried replacing the while statement with this:

while (resp_leng > 0)

And it just loops forever. It might be because the connection was not closed on the PHP side but I have no loops at all running on my PHP script so that can't be it.

Really confused on why this isn't working.