Example of SmartBuffer and SocketGroup. Retrieves a web page and prints it.
#include <netlink/socket.h>
#include <netlink/socket_group.h>
#include <netlink/smart_buffer.h>
#include <string>
#include <iostream>
using std::string;
using std::cout;
}
};
}
};
int main(int argc, char *argv[]) {
if (argc < 2){
cout << "\n Use:\n\t" << argv[0] << " URL\n";
return 0;
}
string url(argv[1]);
if(!url.substr(0,7).compare("http://"))
url = url.substr(7);
size_t slashPosition = url.find('/');
string host, path;
if(slashPosition != string::npos) {
host = url.substr(0, slashPosition);
path = url.substr(slashPosition);
}
else {
host = url;
path = "/";
}
cout << "\nHost: " << host << "\tPath: " << path;
try {
string request = "GET " + path + " HTTP/1.0\r\n";
request += "Host: " + host + "\r\n";
request += "User Agent: webGet - NetLink Sockets C++ Library Example\r\n\r\n";
socket.send(request.c_str(), request.length());
OnRead onRead;
OnDisconnect onDisconnect;
cout << "\n";
cout.write((
const char*)*buffer, buffer.
size());
}
cout <<
"\nError: " << e.
what();
}
return 0;
}