Вот фрагмент с комментариями, чтобы показать вам, как сделать простой запрос GET в C (Обратите внимание: я написал это с учетом POSIX)!:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<netinet/in.h>
#define flags 0
int main() {
int sock; // Sockets are int data type
sock = socket(AF_INET, SOCK_STREAM, 0); //AF_INET = Internet Connection //SOCK_STREAM = TCP
struct sockaddr_in server_host; //Struct type is a data type consisting of other data types inside. Similar to a 'class'
struct timeval timeout;
timeout.tv_sec = 1; //Set Socket Timeout After 1 Second
timeout.tv_usec = 0; //Therefore it will stop sending/receiving data
setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout));
server_host.sin_family = AF_INET; //Family type of the connection
server_host.sin_addr.s_addr = inet_addr("216.58.212.110"); //google.com's IP Address
server_host.sin_port = htons(80); //HTTP Port = 80
connect(sock, (struct sockaddr*)&server_host, sizeof(server_host));
//This statement means:
//Connect using the socket - connect(sock...
//Convert 'server_host' to a 'sockaddr' type - (struct sockaddr*)&server_host...
//With a memory size of 'server_host' (however big it is) - sizeof(server_host)
const char get_request[] = "GET / HTTP/1.0\r\n\r\n"; //This is a GET request. It asks for the site.
send(sock, get_request, strlen(get_request), flags); //Send the get request
//Send using socket 'sock' - send(sock...
//Send data 'get_request' - get_request, ...
//How long 'get_request' is - strlen(get_request), ...
char * received_data = (char *)malloc(sizeof(char)*10000); //Gives 'received_data' 10000 From the RAM
//recv(sock, received_data, 4096, flags);
//Receive data using socket 'sock' - recv(sock...
//Put the data into 'received_data' - received_data, ...
//How much of 'received_data' to take up - 4096, ...
do {
char byte[1];
recv(sock, byte, 1, flags);
if(strcmp(byte, "") == 0) {
break;
} else {
strcat(received_data, byte);
memset(byte,'\0',sizeof(byte));
}
} while(1==1);
puts(received_data);
close(sock);
free(received_data); //REMEMBER TO DO THIS!!!!
return 0;
}
Надеюсь, это поможет!