codice:
/*
client_http.c -- un semplice client http
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#define MAXDATASIZE 1000 // massimo numero di bytes che possiamo ricevere in una sola volta
int main(int argc, char *argv[])
{
int sockfd, numbytes;
int portno = 80; // la porta di comunicazione
char buf[MAXDATASIZE];
struct hostent *he;
struct sockaddr_in their_addr; // connector's address information
if (argc < 2) {
fprintf(stderr,"Utilizzo %s Nome host (www.mio_server.com)\n", argv[0]);
exit(0);
}
if ((he = gethostbyname(argv[1])) == NULL) { // get the host info
perror("gethostbyname");
exit(1);
}
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket");
exit(1);
}
their_addr.sin_family = AF_INET;
their_addr.sin_port = htons(portno);
their_addr.sin_addr = *((struct in_addr *) he->h_addr);
memset(their_addr.sin_zero, '\0', sizeof their_addr.sin_zero);
if (connect(sockfd, (struct sockaddr *)&their_addr, sizeof their_addr) == -1) {
perror("connect");
exit(1);
}
char request[256];
char buffer[256];
int n;
printf("Introdurre la richiesta, prego : ");
bzero(buffer,256);
fgets(request, 255, stdin);
strcpy(buffer, "GET ");
strcat(buffer, request);
strcat(buffer, " HTTP1.1");
strcat(buffer, "\n");
n = write(sockfd, buffer, strlen(buffer));
if (n < 0)
perror("Errore di scrittura al socket");
if ((numbytes=recv(sockfd, buf, MAXDATASIZE-1, 0)) == -1) {
perror("Errore di comunicazione nel ricevere i dati dal socket");
exit(1);
}
buf[numbytes] = '\0';
printf(buf);
printf("\n");
return 0;
}
studiati questo codice che è una bozza di client http. Modifica MAXDATASIZE secondo le tue esigenze.
Il testo che richiedi lo trovi nel buffer buf, da li in poi ne fai quello che vuoi, per adesso è semplicemente inviato allo standard out.
ciao
sergio