Wasim Bnayat ...
Temat: klient http
Marcin zobacz, zrobiłem prosty klient który pobiera żądany plik z wybranego serwra internetowego działa:P#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <netdb.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define BUFSIZE 512
int main(int argc, char *argv[])
{
int gniazdo;
char bufor[BUFSIZE];
unsigned short port;
struct sockaddr_in srvaddr;
struct hostent *host;
int temp=1;
if( (argc<3) || (argc >4) )
{
fprintf(stderr,"********************************************\n");
fprintf(stderr,"Uzycie:\t %s adres nazwa_pliku\n",argv[0]);
fprintf(stderr,"********************************************\n");
exit(1);
}
host=gethostbyname(argv[1]);
if(!host) {fprintf(stderr,"gethostbyname()\n");exit(1);}
else
{
memset(&srvaddr,0,sizeof(srvaddr));
bcopy(host->h_addr_list[0], (char *) &srvaddr.sin_addr, host->h_length);
}
sprintf(bufor,"GET http://%s/%s",argv[1],argv[2]);
port=80;
if((gniazdo=socket(PF_INET,SOCK_STREAM,0))<0)
{
perror("Socket()\n");
fprintf(stderr,"%s\n",sys_errlist[errno]);
exit(1);
}
srvaddr.sin_family=AF_INET;
srvaddr.sin_port=htons(port);
if(connect(gniazdo,(struct sockaddr *) &srvaddr, sizeof(srvaddr))<0)
{
perror("Connect()\n");
fprintf(stderr,"%s\n",sys_errlist[errno]);
exit(1);
}
if(write(gniazdo, bufor, sizeof(bufor))<0)
{
fprintf(stderr,"%s\n",sys_errlist[errno]);
exit(1);
}
shutdown(gniazdo,SHUT_WR);
while(temp>0)
{
memset(bufor,'\0',BUFSIZE);
if((temp=read(gniazdo, bufor, BUFSIZE)) < 0 )
{
fprintf(stderr,"%s\n",sys_errlist[errno]);
exit(1);
}
printf("%s",bufor);
}
printf("\n");
close(gniazdo);
exit(0);
return 0;
}