c - Request webpage using url instead of ip address -
how can request webpage using url instead of ip address? when tried use url got error message.
i using android application called c4droid , not have many libraries. many other examples needed libraries did not have, can using libraries included in c4droid?
#include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <string.h> #include <stdio.h> #include <stdlib.h> int main(void) { int socket_handle; struct sockaddr_in socket_detials; char *input_buffer; char *pinput_buffer; ssize_t bytes_received; ssize_t bytes_sent; char *phttpget; char *httpget = "get / http/1.0\r\n" "host: www.google.com\r\n" "\r\n"; phttpget = httpget; bytes_sent = 0; input_buffer = malloc(1024); if (input_buffer == null) { printf("sorry, couldnt allocate memory input buffer\n"); return -1; } memset(input_buffer, 0, 1024); memset(&socket_detials, 0, sizeof(struct sockaddr_in)); socket_handle = socket(af_inet, sock_stream, 0); if (socket_handle == -1) { printf("could not create socket\n"); return -1; } socket_detials.sin_family = af_inet; socket_detials.sin_addr.s_addr = inet_addr("74.125.224.72"); //google's ip adress socket_detials.sin_port = htons(80); if (connect (socket_handle, (struct sockaddr *)&socket_detials, sizeof(struct sockaddr)) == -1) { printf("couldnt connect server\n"); return -1; } printf("attempting send %zd bytes server\n", strlen(httpget)); (;;) { bytes_sent = send(socket_handle, phttpget, strlen(phttpget), 0); if (bytes_sent == -1) { printf("an error occured sending data\n"); return -1; } if (httpget + strlen(httpget) == phttpget) break; phttpget += bytes_sent; } (;;) { bytes_received = recv(socket_handle, input_buffer, 1023, 0); if (bytes_received == -1) { printf("an error occured during receive procedure \n"); return 0; } if (bytes_received == 0) break; pinput_buffer = input_buffer + bytes_received; *pinput_buffer = 0; printf("%s", input_buffer); } printf("\nfinished receiving data\n"); return 0; }
Comments
Post a Comment