67 lines
1.4 KiB
C
67 lines
1.4 KiB
C
/*
|
|
spb - a very simple toy spartan client
|
|
*/
|
|
#include <stdio.h> //printf
|
|
#include <string.h> //strlen
|
|
#include <sys/socket.h> //socket
|
|
#include <arpa/inet.h> //inet_addr
|
|
#include <unistd.h>
|
|
#include <netdb.h>
|
|
#include "global.h"
|
|
|
|
char req[1000];
|
|
char reply[0x10000];
|
|
|
|
long hostname_to_ip(char * hostname){
|
|
struct hostent *he;
|
|
struct in_addr **addr_list;
|
|
|
|
if ( (he = gethostbyname( hostname ) ) == NULL) {
|
|
herror("gethostbyname");
|
|
return 0;
|
|
}
|
|
addr_list = (struct in_addr **) he->h_addr_list;
|
|
// printf("hostname_to_ip: ok; %d bytes\n",he->h_length);
|
|
//printf("0: %08lX\n",*(long*)addr_list[0]);
|
|
return *(long*)addr_list[0];
|
|
}
|
|
|
|
|
|
struct sockaddr_in server;
|
|
|
|
int get(FILE* f,char* host, U32 port,char* path){
|
|
|
|
int sock = socket(AF_INET , SOCK_STREAM , 0);
|
|
if (sock == -1){
|
|
printf("Could not create socket\n");
|
|
return -1;
|
|
}
|
|
|
|
|
|
if(!(server.sin_addr.s_addr = hostname_to_ip(host)))
|
|
return -2;
|
|
server.sin_family = AF_INET;
|
|
server.sin_port = htons(port?port:300);
|
|
|
|
if (connect(sock , (struct sockaddr*)&server , sizeof(struct sockaddr_in)) < 0) {
|
|
perror("Could not connect\n");
|
|
return -3;
|
|
}
|
|
|
|
sprintf(req,"%s %s 0\r\n",host, path);
|
|
|
|
//Send some data
|
|
if( send(sock , req , strlen(req) , 0) < 0) {
|
|
puts("Send failed");
|
|
return -4;
|
|
}
|
|
|
|
//Receive a reply from the server
|
|
ssize_t len;
|
|
while( (len = recv(sock, reply, sizeof(reply), 0)) > 0){
|
|
fwrite(reply,len,1,f);
|
|
}
|
|
|
|
return 0;
|
|
}
|