Thread: Segmentation fault problem

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    11

    Unhappy Segmentation fault problem

    I am trying to produce a c program that downloads a web page using sockets, however when i compile i get a warning message:

    webclient.c: In function `main':
    webclient.c:61: warning: passing arg 2 of `connect' from incompatible
    pointer type

    Then when i run the program i get a segmentation fault

    I cant work out why ,

    heres the code:
    Code:
    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netdb.h>
    #include <netinet/in.h>
    
    #define PROTOCOL "tcp"
    #define SERVICE "http"
    #define GET "GET / HTTP/1.0\n\n"
    
    int main(int argc, char *argv[]) {
    	int sockid;
    	int bufsize;
    	char host[50];
    	char buffer[1024];
    	struct sockaddr_in socketaddr;
    	struct hostent *hostaddr;
    	struct servent *servaddr;
    	struct protoent *protocol;
    
    	strcpy(host, argv[1]);
    
    	/* Resolve the host name */
    	if (!(hostaddr = gethostbyname(host))) {
    		fprintf(stderr, "Error resolving host.");
    		exit(1);
    	}
    
    	/* clear and initialize socketaddr */
    	memset(&socketaddr, 0, sizeof(socketaddr));
    	socketaddr.sin_family = AF_INET;
    
    	/* setup the servent struct using getservbyname */
    	servaddr = getservbyname(SERVICE, PROTOCOL);
    	socketaddr.sin_port = servaddr->s_port;
    
    	memcpy(&socketaddr.sin_addr, hostaddr->h_addr, hostaddr->h_length);
    
    	/* protocol must be a number when used with socket()
    	since we are using tcp protocol->p_proto will be 0 */
    	protocol = getprotobyname(PROTOCOL);
    
    	sockid = socket(AF_INET, SOCK_STREAM, protocol->p_proto);
    	if (sockid < 0) {
    		fprintf(stderr, "Error creating socket.");
    		exit(1);
    	}
    
    	/* everything is setup, now we connect */
    	if(connect(sockid, &socketaddr, sizeof(socketaddr)) == -1) {
    		fprintf(stderr, "Error connecting.");
    		exit(1);
    	}
    
    	/* send our get request for http */
    	if (send(sockid, GET, strlen(GET), 0) == -1) {
    		fprintf(stderr, "Error sending data.");
    		exit(1);
    	}
    
    	/* read the socket until its clear then exit */
    	while ( (bufsize = read(sockid, buffer, sizeof(buffer) - 1))) {
    		write(1, buffer, bufsize);
    	}
    
    	close(sockid);
    }

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Please keep related problems to the same thread
    http://cboard.cprogramming.com/showthread.php?t=65302

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault problem
    By odedbobi in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2008, 03:36 AM
  2. Strange segmentation fault
    By Ron in forum C Programming
    Replies: 24
    Last Post: 06-15-2008, 02:10 PM
  3. Segmentation Fault hmm?
    By pobri19 in forum C Programming
    Replies: 4
    Last Post: 05-03-2008, 07:51 AM
  4. Re: Segmentation fault
    By turkish_van in forum C Programming
    Replies: 8
    Last Post: 01-20-2007, 05:50 PM
  5. [C++] Segmentation Fault {Novice C++ Programmer}
    By INFERNO2K in forum C++ Programming
    Replies: 24
    Last Post: 06-08-2005, 07:44 PM