Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <netdb.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- int main(int argc, char** argv)
- {
- struct sockaddr_in servaddr;
- struct hostent *hp;
- int sock_id;
- char message[1024*1024] = {0};
- int msglen;
- char request[] = "GET /index.html HTTP/1.0\n"
- "From: slava!!!\nUser-Agent: wget_sortof by slava\n\n";
- //Get a socket
- if((sock_id = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
- fprintf(stderr,"Couldn't get a socket.\n"); exit(EXIT_FAILURE);
- }
- else {
- fprintf(stderr,"Got a socket.\n");
- }
- //book uses bzero which my man pages say is deprecated
- //the man page said to use memset instead. :-)
- memset(&servaddr,0,sizeof(servaddr));
- //get address for google.com
- if((hp = gethostbyname("google.com")) == NULL) {
- fprintf(stderr,"Couldn't get an address.\n"); exit(EXIT_FAILURE);
- }
- else {
- fprintf(stderr,"Got an address.\n");
- }
- //bcopy is deprecated also, using memcpy instead
- memcpy((char *)&servaddr.sin_addr.s_addr, (char *)hp->h_addr, hp->h_length);
- //fill int port number and type
- servaddr.sin_port = htons(80);
- servaddr.sin_family = AF_INET;
- //make the connection
- if(connect(sock_id, (struct sockaddr *)&servaddr, sizeof(servaddr)) != 0) {
- fprintf(stderr, "Couldn't connect.\n");
- }
- else {
- fprintf(stderr,"Got a connection!!!\n");
- }
- //NOW THE HTTP PART!!!
- //send the request
- write(sock_id,request,strlen(request));
- //read the response
- msglen = read(sock_id,message,1024*1024);
- printf("response is %d bytes long\n", msglen);
- //print the reasponse
- printf("%s", message);
- return 0;
- }
Add Comment
Please, Sign In to add comment