I'm trying to make a simple web client using sockets. I send a GET request to a web page and want to receive an HTML file. I know I can receive using recv()
, but I want to do this using a FILE
. I'm using fdopen
to wrap the socket in a FILE
. Now I can use fgetc()
to read the response. However, fgets()
fails - maybe I'm using it wrong. Any way to easily read the HTTP response line by line?
This works:
FILE *input = fdopen(mySocket, "r");
while(!feof(input))
{
printf("%c", fgetc(input));
}
I need something along these lines:
FILE *input = fdopen(mySocket, "r");
char c[2000];
while(fgets(c, sizeof(c), input) != NULL)
{
printf("%s\n", c);
}
If there is any other convenient way to read the response, please advise.
I just added error checking and it works now. The input was indeed NULL because my request never went through. Thanks!
FILE *input= fdopen(mySocket, "r");
if(input== NULL)
{
printf("Error");
exit 1;
}
char c[2000];
while(fgets(c, sizeof(c), input) != NULL)
{
printf("%s\n", c);
}