简体   繁体   中英

C socket recieves data then does not send it

i have created a simple socket connection which sends a request to a node.js app and receives some random data and then sends back the data but it sends and receives the data correctly but does not send back the random data

char *httpbody;
struct addrinfo hints, *result;
const char *host = "localhost";
char user[512] = "test@gmail.com";
char pass[512] = "test";
char headers[512];
srand(time(NULL));

memset(&hints, 0, sizeof(hints));

hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;

if (getaddrinfo(host, "3000", &hints, &result) != 0) {
    freeaddrinfo(result);
    perror("Could not resolve hostname.");
    exit(1);
}

int newsocket = socket(result->ai_family, result->ai_socktype, 0);
if (newsocket == -1) {
    perror("Could not create socket.");
    freeaddrinfo(result);
    close(newsocket);
    exit(1);
}

if (connect(newsocket, result->ai_addr, result->ai_addrlen) == -1) {
    perror("Could not connect.");
    freeaddrinfo(result);
    close(newsocket);
    exit(1);
}

sprintf(headers, "GET /auth?user=%s&pass=%s HTTP/1.1\r\n"
        "User-Agent: Device\r\n"
        "Accept-Language: de,en;q=0.7,en-us;q=0.3\r\n"
        "Content-Type: text/html; charset=UTF-8\r\n"
        "Connection: Close\r\n\r\n", user, pass);

if ((send(newsocket, headers, strlen(headers), 0)) == -1) {
    perror("Could not send data.");
    freeaddrinfo(result);
    close(newsocket);
    exit(1);
}

char response[51201] = "";
recv(newsocket, response, 51200, 0);
response[strlen(response)] = '\0';

httpbody = strstr(response, "\r\n\r\n");
if (httpbody) {
    httpbody += 8;
}

int data = rand() % 255 + 1;

sprintf(headers, "GET /add_data?user=%s&data=%i&"
        "uid=%s HTTP/1.1\r\n"
        "User-Agent: Device\r\n"
        "Accept-Language: de,en;q=0.7,en-us;q=0.3\r\n"
        "Content-Type: text/html; charset=UTF-8\r\n"
        "Connection: Close\r\n\r\n", user, data, httpbody);

if ((send(newsocket, headers, strlen(headers), 0)) == -1) {
    perror("Could not send data.");
    freeaddrinfo(result);
    close(newsocket);
    exit(1);
}

printf("done");

freeaddrinfo(result);
close(newsocket);

when i remove the revc request it works fine and it does not show any errors and i am completely lost

Check this lines of code:

recv(newsocket, response, 51200, 0);
response[strlen(response)] = '\0';`

recv doesn't return null-terminated data, so you cannot use strlen for determine size of data. You must use return value of recv instead:

int data_size = recv(newsocket, response, 51200, 0);

An HTTP connection ends each time when you specify "Connection: close" in HTTP 1.1, thus the socket is not valid after you finish reading the response from the server.

Possible solutions are:

  • Remove "Connection: close" header from the first request
  • Connect() to the server again after you recv() the response

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM