1

I wrote This code in Linux platform.

When this program was run, It was expect to print HTTP header and html content. Just like:

HTTP/1.1 200 OK\r\n
...
...
<!DOCTYPE html>
...
</html>

But when I run it, I found it just print HTTP header

This is my C code:

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netdb.h>  // for struct sockaddr_in AF_IN
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>  // for inet_pton
#include <netinet/in.h> // for IPPROTO_TCP

#define PORT "80"
#define BAIDU_IP "119.75.217.56"
#define GET_INFO "GET / HTTP/1.1\r\nHost: www.baidu.com:80\r\n\r\n"

int main()
{
    int sock_local;
    in_port_t port = atoi(PORT);
    struct sockaddr_in server;

    memset(&server, 0, sizeof(server));
    server.sin_family = AF_INET;
    server.sin_port = htons(port);

    sock_local = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    assert(sock_local > 0);

    int retVal = -1;
    retVal = inet_pton(AF_INET, BAIDU_IP, &server.sin_addr.s_addr);
    assert(retVal == 1);

    if (connect(sock_local, (struct sockaddr*)&server, sizeof(server)) < 0)
    {
        perror("connect");
        exit(EXIT_FAILURE);
    }

    ssize_t nByte = send(sock_local, GET_INFO, strlen(GET_INFO), 0);
    if( nByte <= 0)
    {
        perror("send");
        exit(EXIT_FAILURE);
    }

    char BUF[BUFSIZ];
    size_t recived_len = 0;
    while ((recived_len = recv(sock_local, BUF, BUFSIZ-1, 0)) > 0)
    {
        int statu;
        BUF[recived_len] = '\0';
        printf("%s", BUF);
        statu = (recived_len==BUFSIZ-1) ? 0 : 1;
        if (statu) break;
    }
    if (recived_len < 0)
    {
        perror("recv");
    }

    close(sock_local);
    return 0;
}

Could you help to recive both HTTP head and 'html content'?

I don't know why it dosen't works.

====================== Solution =============================

I'm very glad to say, I found solution to this problem by myself.

I delete if (statu) break;, then it works as I expect!

4
  • BUFSIZ was defined as 8192. HTTP header has 387 byte Commented Apr 30, 2012 at 3:51
  • What do the printed headers look like? Was an HTTP 200 response code returned, or was it another response code, maybe a 3xx redirection to another URL? Commented Apr 30, 2012 at 4:18
  • size_t is unsigned. The recv() and read() syscalls can return a -1 value on error, which will confuse your programs logic. Commented Apr 30, 2012 at 17:54
  • htons() wants an integer argument, and PORT is a string constant. Commented Apr 30, 2012 at 18:26

1 Answer 1

3

The problem seems to be right here:

statu = (recived_len==BUFSIZ-1) ? 0 : 1;
if (statu) break;

I don't know why you have included this code. It seems to exit the loop unless you filled the buffer completely on the previous read. What is your reason for doing this?

Delete those two lines of code, and it should work.

(Oh, and by the way, that's not how you spell "received".)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.