From: Bill Cunningham on

"John Gordon" <gordon(a)panix.com> wrote in message
news:hsscsp$cqm$1(a)reader1.panix.com...

> Tell us what line of code generates the seg fault.
> Tell us what's in the mysterious header file main.h.
Main.h

#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>

cl.c
#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) {
perror("socket");
return -1;
}
if ((c = connect(sock, p->ai_addr, p->ai_addrlen)) == -1) {
perror("connect");
return -2;
}
fcntl(sock, F_SETFL, O_NONBLOCK);
if ((rv = write(sock, buf, sizeof buf)) == -1) {
perror("write");
return -3;
}
return 0;
}

All I get when I add main() and call the client() function is a sstring of
decimals.

int main(){

int r;
printf("%d\n",r);
}

And I also include the header func.h that I wrote that defines this.

int client(char *, char *);

Bill



From: Bill Cunningham on

"Bill Cunningham" <nospam(a)nspam.invalid> wrote in message
news:4bf1bfb1$0$12459$bbae4d71(a)news.suddenlink.net...

[...]

> int main(){
>
> int r;
> printf("%d\n",r);
> }
>
> And I also include the header func.h that I wrote that defines this.
>
> int client(char *, char *);


Well I forgot r=client(NULL,"7");
but the results are the same.
>


From: Bill Cunningham on
That fcntl() seems suspicious to me. Is that like using a function like
bind or listen or accept on a client socket? Maybe it shouldn't be there. I
know it keeps a socket from sleeping after a listen call. I'm going to error
check it anyway.

Bill
be back...


From: Ian Collins on
On 05/18/10 10:40 AM, Bill Cunningham wrote:
> That fcntl() seems suspicious to me. Is that like using a function like
> bind or listen or accept on a client socket? Maybe it shouldn't be there. I
> know it keeps a socket from sleeping after a listen call. I'm going to error
> check it anyway.

Using functions at random are we?

--
Ian Collins
From: Scott Lurndal on
"Bill Cunningham" <nospam(a)nspam.invalid> writes:
> 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);
>}
>

type 'ulimit -c unlimited' in your shell to get a core file when
your program fails.

hint: man getaddrinfo, and pay special attention to the node argument.

scott