I wrote a basic C program that establishes a connection with the IP address of google.com
and then starts receiving data from client.
But firstly, during compilation the following warning is generated:
test.c:12:25: warning: implicit declaration of function 'inet_addr' is invalid in C99 [-Wimplicit-function-declaration] address.sin_addr.s_addr=inet_addr("2... ^ 1 warning generated.
Secondly, on running the program, the following output is generated:
HTTP/1.0 400 Bad Request Content-Type: text/html; charset=UTF-8 Referrer-Policy: no-referrer ContGET/HTTP/1.1
What changes can I make in my code to successfully receive data from an HTTP server ?
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<stdlib.h>
int main()
{
int mySocket = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in address;
address.sin_port = htons(80);
address.sin_family = AF_INET;
address.sin_addr.s_addr = inet_addr("216.58.200.196");
connect(mySocket, (struct sockaddr *)&address, sizeof(address));
char msg[21] = "GET/HTTP/1.1\r\n\r\n";
send(mySocket, msg, 21, 0);
char msgRecv[100];
recv(mySocket, msgRecv, 100, 0);
printf("%s", msgRecv);
}