Prev: Programming UI-based shell/Terminal programs
Next: open all bloced sites with free proxy service facebook,myspace,twitter ......etc
From: Bill Cunningham on 17 May 2010 16:03 Why do I keep getting a segmentation fault here? I want to write to an echo server and receive back. #include "main.h" struct addrinfo ad, *p; int client(char *ip, char *port) { int rv, c, sock; char *buf = "hello\n"; memset(&ad, 0, sizeof ad); getaddrinfo(ip, port, &ad, &p); ad.ai_family = AF_INET; ad.ai_socktype = SOCK_STREAM; if ((sock = socket(p->ai_family, p->ai_socktype, 0)) == -1) return -1; if ((c = connect(sock, p->ai_addr, p->ai_addrlen)) == -1) return -2; fcntl(sock, F_SETFL, O_NONBLOCK); if ((rv = write(sock, buf, sizeof buf)) == -1) return -3; return 0; } int main() { int r; r = client(NULL, "4"); printf("%d\n", r); }
From: Ian Collins on 17 May 2010 16:27 On 05/18/10 08:03 AM, Bill Cunningham wrote: > Why do I keep getting a segmentation fault here? Where? <snip> -- Ian Collins
From: Bill Cunningham on 17 May 2010 16:47 "Ian Collins" <ian-news(a)hotmail.com> wrote in message news:85dn5eF15dU1(a)mid.individual.net... > On 05/18/10 08:03 AM, Bill Cunningham wrote: >> Why do I keep getting a segmentation fault here? > > Where? > > <snip> #include "main.h" struct addrinfo ad, *p; int client(char *ip, char *port) { int rv, c, sock; char *buf = "hello\n"; memset(&ad, 0, sizeof ad); getaddrinfo(ip, port, &ad, &p); ad.ai_family = AF_INET; ad.ai_socktype = SOCK_STREAM; if ((sock = socket(p->ai_family, p->ai_socktype, 0)) == -1) return -1; if ((c = connect(sock, p->ai_addr, p->ai_addrlen)) == -1) return -2; fcntl(sock, F_SETFL, O_NONBLOCK); if ((rv = write(sock, buf, sizeof buf)) == -1) return -3; return 0; } All this above compiles fine into an object file. int main() { int r; r = client(NULL, "4"); printf("%d\n", r); } This is compiled with the object file and a header saying int client(char*,char*); When I run the resulting executable I get a segmentation fault. Firstly that "4" should be "7" for an echo server.
From: David Schwartz on 17 May 2010 17:02 On May 17, 1:47 pm, "Bill Cunningham" <nos...(a)nspam.invalid> wrote: > When I run the resulting executable I get a segmentation fault. Firstly that > "4" should be "7" for an echo server. You need to generate and analyze a core dump to make sense of the segmentation fault. Or at least add printf's or similar commands so you can figure out which line is faulting. DS
From: Bill Cunningham on 17 May 2010 17:32
"David Schwartz" <davids(a)webmaster.com> wrote in message news:cfe79619-dc92-4339-9614-b5aa0d58a434(a)v12g2000prb.googlegroups.com... You need to generate and analyze a core dump to make sense of the segmentation fault. Or at least add printf's or similar commands so you can figure out which line is faulting. I don't know how to do that. I'm not getting a core dump. Should I generate one somehow? I do have gdb. So then the source code looks fine there's just something happening somewhere? I don't know what a segmentation fault means though I've seen many of them. Bill |