JavaOdd
JavaOdd

Reputation: 75

Bug keep-alive delay

This code connect using keep-alive to load lots of domains that share the same IP Address, but there's a bug after the first 25 domains, witch is a 30 seconds delay at every domain load! How to solve this?

#pragma comment(lib,"ws2_32.lib")
#include <WinSock2.h>
#include <iostream>
int main(){
    WSAData wsaData;
    WORD DllVersion = MAKEWORD(2, 1);
    if (WSAStartup(DllVersion, &wsaData) != 0) {
        MessageBoxA(NULL, "Winsock startup failed", "Error", MB_OK | MB_ICONERROR);
        exit(1);
    }
    SOCKADDR_IN addr;
    int sizeofaddr = sizeof(addr);
    addr.sin_addr.s_addr = inet_addr("123.123.123.123");
    addr.sin_port = htons(80);
    addr.sin_family = AF_INET;
    SOCKET Connection = socket(AF_INET, SOCK_STREAM, NULL);
    if (connect(Connection, (SOCKADDR*)&addr, sizeofaddr) != 0)
    {
        MessageBoxA(NULL, "Failed to Connect", "Error", MB_OK | MB_ICONERROR);
        return 0;
    }
    std::cout << "Connected!" << std::endl;
    char MTD[] = "GET / HTTP/1.1\r\nHost:site.com\r\nConnection:keep-alive\r\n\r\nGET / HTTP/1.1\r\nHost:site.com\r\nConnection:keep-alive\r\n\r\nGET / HTTP/1.1\r\nHost:site.com\r\nConnection:keep-alive\r\n\r\nGET / HTTP/1.1\r\nHost:site.com\r\nConnection:keep-alive\r\n\r\nGET / HTTP/1.1\r\nHost:site.com\r\nConnection:keep-alive\r\n\r\nGET / HTTP/1.1\r\nHost:site.com\r\nConnection:keep-alive\r\n\r\nGET / HTTP/1.1\r\nHost:site.com\r\nConnection:keep-alive\r\n\r\nGET / HTTP/1.1\r\nHost:site.com\r\nConnection:keep-alive\r\n\r\nGET / HTTP/1.1\r\nHost:site.com\r\nConnection:keep-alive\r\n\r\nGET / HTTP/1.1\r\nHost:site.com\r\nConnection:close\r\n\r\n";
    send(Connection, MTD, sizeof(MTD), NULL);
    char MOTD[1];
    int result;
    while ((result = recv(Connection, MOTD, 1, NULL)) > 0){
        std::cout << MOTD[0]; // ...
    }
    if (result == 0) {
        // end of stream
        std::cout << "end of stream";
        close(Connection);
    } else if (result < 0) {
        // error
        std::cout << "error";
        perror("recv"); // at least
        close(Connection);
    } else {
        std::cout << "data received";
        // data received, in MOTD[0..result-1]
    }
};

Upvotes: 0

Views: 205

Answers (1)

user207421
user207421

Reputation: 310957

result=recv(Connection, MOTD, 1, NULL);
while(recv(Connection, MOTD, 1, NULL)){

This makes no sense whatsoever. The first recv() reads at least one byte into the buffer or sets result to zero or -1, whereupon you immediately ignore whatever it did and issue another recv(), whose result you ignore. It should be more like this:

while ((result = recv(Connection, MOTD, 1, NULL)) > 0){
    // data received, in MOTD[0..result-1]
}
if (result == 0) {
    // end of stream
    close(Connection);
} else if (result < 0) {
    // error
    perror("recv"); // at least
    close(Connection);
}

NB You aren't using HTTP keepalive at all. You are creating a new socket for every request and never closing it, at least until you use the code in this answer. You are also telling the server via the Connection: keepalive header to keep that connection alive, which will merely waste server resources. Either use HTTP keep-alive properly or close the socket after each request.

Upvotes: 2

Related Questions