From: Bill Cunningham on
I erased the original client attempt and re-wrote it. I get a seg fault
again. It is interesting though when I turn on my modem and a connection is
made I get an error concerning connect() and operation in progress. There is
an error in the program somewhere. I have alot more reading to do but in
short I must need (or if I mustn't need it would be a good idea to have) two
sockets. One for recv and one for send. Is fcntl() needed here since poll()
is being used ? Here I am locking in TCP and that might not be a good idea.
#include "main.h"

static struct pollfd po;
static struct addrinfo ad, *p;
static int rv, sock, con;
static char buf[1024] = "hello\n", buf2[1024];

int client(char *ip, char *port)
{
memset(&ad, 0, sizeof ad);
ad.ai_family = PF_INET;
ad.ai_socktype = SOCK_STREAM;
ad.ai_protocol = IPPROTO_TCP;
getaddrinfo(ip, port, &ad, &p);
sock = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
fcntl(sock, F_SETFL, O_NONBLOCK);
if ((con = connect(sock, p->ai_addr, p->ai_addrlen)) == -1) {
perror("connect error");
return -1;
}
freeaddrinfo(&ad);
po.fd = sock;
po.events = POLLOUT | POLLIN;
rv = poll(&po, 1, 3500);
if (rv == -1) {
perror("poll error");
return -1;
}
if (rv == 0) {
puts("timed out");
return 1;
}
if (po.revents & POLLOUT) {
send(sock, buf, strlen(buf) + 1, 0);
return 0;
}
if (po.revents & POLLIN) {
recv(sock, buf2, strlen(buf2) + 1, 0);
return printf("%s\n", buf2);
}
return 0;
}



From: Moi on
On Tue, 04 May 2010 10:21:29 -0400, Bill Cunningham wrote:

> I erased the original client attempt and re-wrote it. I get a seg fault
> again. It is interesting though when I turn on my modem and a connection
> is made I get an error concerning connect() and operation in progress.
> There is an error in the program somewhere. I have alot more reading to
>
> recv(sock, buf2, strlen(buf2) + 1, 0); return printf("%s\n", buf2);
----------------------------^^^^^^^^^^^^

Makes no sense.

There you are.

HTH,
AvK
From: Bill Cunningham on

"Moi" <root(a)invalid.address.org> wrote in message
news:46b00$4be13194$5350c024$6012(a)cache110.multikabel.net...

>> recv(sock, buf2, strlen(buf2) + 1, 0); return printf("%s\n",
>> buf2);
> ----------------------------^^^^^^^^^^^^
>
> Makes no sense.
>
> There you are.
I noticed that the 3rd argument of recv was a size_t. Exactly what
strlen() returns. I thought it might work. The +1 was for '\0'.

Bill


From: Moi on
On Thu, 06 May 2010 14:55:18 -0400, Bill Cunningham wrote:

> "Moi" <root(a)invalid.address.org> wrote in message
> news:46b00$4be13194$5350c024$6012(a)cache110.multikabel.net...
>
>>> recv(sock, buf2, strlen(buf2) + 1, 0); return printf("%s\n",
>>> buf2);
>> ----------------------------^^^^^^^^^^^^
>>
>> Makes no sense.
>>
>> There you are.
> I noticed that the 3rd argument of recv was a size_t. Exactly what
> strlen() returns. I thought it might work. The +1 was for '\0'.


buf2 is uninitialized. Measuring its length at this point just does not make sense.
Maybe sizeof is what you need.
BTW: the +1 makes no sense, either.
Also: check the returnvalue for recv() You might need it.

HTH,
AvK
From: Bill Cunningham on
Moi wrote:
> On Thu, 06 May 2010 14:55:18 -0400

> buf2 is uninitialized. Measuring its length at this point just does
> not make sense. Maybe sizeof is what you need.
> BTW: the +1 makes no sense, either.
> Also: check the returnvalue for recv() You might need it.

Ok I'll check the return of recv(). What if I did this to buf2? char
buf2[1024]={0}; ? Then it would be initialized. Would strlen() work then? I
guess this is more of a C question now. So sizeof works with uninitialized
data and strlen() doesn't?

Bill