Thanks for the tip. I've tried that already, but what happens is I get an endless loop. I guess it's time you better look at my code, so here it is:
Code:
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <fcntl.h>
#include <unistd.h>
#include <iostream>
using std::cin;
using std::cout;
using std::string;
using std::endl;
#define DEST_IP "74.52.33.82"
#define DEST_ADDR "google.com"
#define DEST_PORT 80
#define BUFFER_SIZE 4000
char *lookup_host(string name);
int open_socket();
int close_socket(int sockfd);
int send_packet(int sockfd, string data);
int recieve_packet(int sockfd, string &data);
char *lookup_host(char *name) {
struct hostent *host;
string ip;
if ((host=gethostbyname(name)) == NULL) {
cout << "Could not look up host.\n";
exit(1);
}
ip = inet_ntoa(*((struct in_addr*) host->h_addr));
return (char *)ip.c_str();
}
int open_socket() {
int sockfd;
struct sockaddr_in dest_addr;
sockfd = socket(PF_INET, SOCK_STREAM, 0); // sock file descriptor
dest_addr.sin_family = AF_INET; // host byte order
dest_addr.sin_port = htons(DEST_PORT); // convert destination port to host byte order
dest_addr.sin_addr.s_addr = inet_addr(lookup_host(DEST_ADDR)); // convert IP address to long type
memset(&(dest_addr.sin_zero), '\0', 8); // clear out junk in rest of struct
// connect to host
if (connect(sockfd, (struct sockaddr *) &dest_addr, sizeof(struct sockaddr)) == -1) {
cout << "Could not connect to host (" << DEST_IP << ", port " << DEST_PORT << ").\n";
exit(1);
}
return sockfd;
}
int close_socket(int sockfd) {
return close(sockfd);
}
int send_packet(int sockfd, string data) {
int total=0;
int length = data.length();
int bytesleft = data.length();
int n;
while (total < length) {
n = send(sockfd, data.c_str()+total, bytesleft, 0);
if (n == -1) break;
total += n;
bytesleft -= n;
}
if (n == -1) {
cout << "Connection closed by host.\n";
exit(1);
}
return 0;
}
int recieve_packet(int sockfd, string &data) {
int bytes_recieved;
char buf[BUFFER_SIZE] = {'\0'};
data=buf;
while (1) {
bytes_recieved = recv(sockfd, buf, BUFFER_SIZE, 0);
if (bytes_recieved == 0) break;
if (bytes_recieved < 0) break;
data+=buf;
}
return 0;
}
int main() {
string request = "GET / HTTP/1.1\nHost: www.google.ca\n\n";
string response;
char buffer[BUFFER_SIZE] = {'\0'};
int bytes_recieved;
int sockfd = open_socket(); // create new sending socket
send_packet(sockfd, request);
recieve_packet(sockfd, response);
close_socket(sockfd);
cout << "Data recieved:\n";
cout << response << endl;
return 0;
}
I also liked your suggestion about using libcurl, however I somehow couldn't get curl functions to resolve when I compiled it. The only libraries produced when compiling libcurl on Mac OS X were libcurl.3.dylib and libcurl.3.la, and adding these to the Xcode project didn't seem to resolve it. So I'm still trying this method, although it would be nice if I could get libcurl working.
Thanks.