Reputation: 1692
I'm totally new to network programming. So I'm working on a program that passes something like this to the socket:
GET /index.html HTTP/1.1\r\n // index.html is the site being read from
Host: www.google.com\r\n // host can be anything
\r\n
Note that /index.html and google.com are just examples. My program replaces these by command-line inputs.
So I store that GET message up there in a string, then use the send() method to send the request to the server. I checked errno and it returns "Success"
Right not I'm stuck and don't know what to do next to read the HTML. I think the RIO library will help me with this, but I don't know how to implement the next steps.
This is what I have so far:
int open_clientfd(char *hostname, int port)
{
int clientfd;
struct hostent *hp;
struct sockaddr_in serveraddr;
if ((clientfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
return -1; /* Check errno for cause of error */
/* Fill in the server's IP address and port */
if ((hp = gethostbyname(hostname)) == NULL)
return -2; /* Check h_errno for cause of error */
bzero((char *) &serveraddr, sizeof(serveraddr));
serveraddr.sin_family = AF_INET;
bcopy((char *)hp->h_addr_list[0],
(char
*)&serveraddr.sin_addr.s_addr,
hp->h_length);
serveraddr.sin_port = htons(port);
/* Establish a connection with
* the server */
if (connect(clientfd, (SA *)
&serveraddr,
sizeof(serveraddr)) < 0)
return -1;
return clientfd;
}
void sendRequest(int clientfd, char request[128]) {
send(clientfd, request, sizeof(request), 0);
fprintf(stderr, "%s\n", strerror(errno)); // return SUCCESS
}
int main(int argc, char **argv) {
int clientfd, port;
char *host, *fileURL, buf[MAXLINE];
rio_t rio;
host = argv[1];
fileURL = argv[2];
port = atoi(argv[3]);
clientfd = Open_clientfd(host, port);
// set up request string
char request[128];
// ....
// now request stores the string above
sendRequest(clientfd, request);
}
Upvotes: 0
Views: 501
Reputation: 5907
In the next step you call recv or read
ssize_t recv(int sockfd, void *buf, size_t len, int flags);
ssize_t read(int fd, void *buf, size_t count);
But you must consider TCP is a stream-oriented protcol. For that reason, the response may come back in one or more TCP messages. You cannot assume it is going to be one and you don't know the size of the response. The return from read/recv may not bring the whole HTTP message. Then you have to read until you reach the end of the HTTP header "\r\n\r\n". After that you have to parse the response obtained so far and find the header Content-Length, obtain the content length and it will give you the size of the HTTP data. Then from the beginning of data, you read Content-Length Bytes.
Upvotes: 2