Created
October 5, 2012 08:38
-
-
Save AlexsJones/3838783 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| string jnx::jnx_network::get_http_page(string host) | |
| { | |
| int sockid; | |
| int bufsize; | |
| char buffer[MAXHTMLBUF]; | |
| struct sockaddr_in socketaddr; | |
| struct hostent *hostaddr; | |
| struct servent *servaddr; | |
| struct protoent *protocol; | |
| if (!(hostaddr = gethostbyname(host.c_str()))) { | |
| throw "jnx_network::get_http_page Unable to resolve host"; | |
| } | |
| memset(&socketaddr, 0, sizeof(socketaddr)); | |
| socketaddr.sin_family = AF_INET; | |
| servaddr = getservbyname(SERVICE, PROTOCOL); | |
| socketaddr.sin_port = servaddr->s_port; | |
| memcpy(&socketaddr.sin_addr, hostaddr->h_addr, hostaddr->h_length); | |
| protocol = getprotobyname(PROTOCOL); | |
| sockid = socket(AF_INET, SOCK_STREAM, protocol->p_proto); | |
| if (sockid < 0) { | |
| throw "jnx_network::get_http_page Unable to create socket"; | |
| } | |
| if(connect(sockid,(struct sockaddr *) &socketaddr, sizeof(socketaddr)) == -1) { | |
| throw "jnx_network::get_http_page Unable to connect"; | |
| } | |
| if (send(sockid, GET, strlen(GET), 0) == -1) { | |
| throw "jnx_network::get_http_page Unable to send data"; | |
| } | |
| while ( (bufsize = read(sockid, buffer, sizeof(buffer) - 1))) { | |
| write(1, buffer, bufsize); | |
| } | |
| close(sockid); | |
| return string(buffer); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment