GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 08-May-2005, 16:53
robsmith robsmith is offline
New Member
 
Join Date: Mar 2005
Posts: 11
robsmith is on a distinguished road
Unhappy

Please help 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:
CPP / C++ / C 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);
}

Last edited by LuciWiz : 08-May-2005 at 23:14. Reason: Please insert your C code between [c] & [/c] tags
  #2  
Old 08-May-2005, 20:34
nkhambal nkhambal is offline
Regular Member
 
Join Date: Jul 2004
Location: CA USA
Posts: 313
nkhambal is a jewel in the roughnkhambal is a jewel in the rough
you need to cast structure socketaddr to struct sockaddr and size should be size of struct sockaddr

CPP / C++ / C Code:
if(connect(sockid, (struct sockaddr *)&socketaddr, sizeof(struct sockaddr)) == -1) {
    fprintf(stderr, "Error connecting.");
    exit(1);
  }

Also http request should be terminated with "\r\n"

so your get request should be

CPP / C++ / C Code:
#define GET "GET / HTTP/1.0\r\n"
 
 

Recent GIDBlogWriting a book by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Graphic problem in Unreal Tournament 2004 zerox Computer Software Forum - Games 10 09-Oct-2005 12:31
Help with segmentation fault boris C Programming Language 1 02-Feb-2005 14:25
segmentation fault in c++ rushman8282 C++ Forum 2 26-Jan-2005 03:38
Segmentation Fault? aaroncohn C++ Forum 2 02-Jan-2005 14:22
child pid xxx exit signal Segmentation fault (11) bezak Apache Web Server Forum 1 24-Nov-2003 09:18

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 20:58.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.