From: Bill Cunningham on
Rainer Weikusat wrote:
> "Bill Cunningham" <nospam(a)nspam.invalid> writes:
>>> Rainer Weikusat wrote:
>>>
>>>> po[1] is the same as *(po + 1) which would access the second
>>>> element of the array po. But your array has only one element.
>>
>> I changed the code to static struct pollfd *po;
>>
>> and renamed the arrays po[0] and re-compiled and got a smooth
>> compilation.
>
> This should result in a segfault at runtime because you are now trying
> to derefences a null pointer. You need to declare a structure, not a
> pointer to a structure. And access the revents-member like this:
>
> po.revents
>
> NB: This is only true for a single struct pollfd, as in your example.

This compiled cleanly-

#include "main.h"

static struct pollfd *po;
static struct addrinfo ad, *p;
static int rv, sock, con;

int client(char *ip, char *port, char msg[1024])
{
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);
po[0].fd = sock;
po[0].events = POLLOUT;
rv = poll(po, 1, 3500);
if (rv == -1) {
perror("poll error");
return -1;
}
if (rv == 0) {
puts("timout error");
return 1;
}
if (po[0].revents & POLLOUT) {
send(sock, msg, strlen(msg) + 1, 0);
puts("success");
return 0;
}
return 0;
}


From: Bill Cunningham on
Rainer Weikusat wrote:
> "Bill Cunningham" <nospam(a)nspam.invalid> writes:
>>> Rainer Weikusat wrote:
>>>
>>>> po[1] is the same as *(po + 1) which would access the second
>>>> element of the array po. But your array has only one element.
>>
>> I changed the code to static struct pollfd *po;
>>
>> and renamed the arrays po[0] and re-compiled and got a smooth
>> compilation.
>
> This should result in a segfault at runtime because you are now trying
> to derefences a null pointer. You need to declare a structure, not a
> pointer to a structure. And access the revents-member like this:
>
> po.revents
>
> NB: This is only true for a single struct pollfd, as in your example.

Yes I did get a seg fault at runtime. It compiled into an object file
fine but when I tried calling the client function from main; I got the seg
fault.

Bill


From: Rainer Weikusat on
"Bill Cunningham" <nospam(a)nspam.invalid> writes:
> Rainer Weikusat wrote:

> #include "main.h"
>
> static struct pollfd *po;

static struct pollfd po;

> static struct addrinfo ad, *p;
> static int rv, sock, con;
>
> int client(char *ip, char *port, char msg[1024])
> {
> 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);
> po[0].fd = sock;
> po[0].events = POLLOUT;

po.fd = sock;
po.events = POLLOUT;

> rv = poll(po, 1, 3500);

rv = poll(&po, 1, 3500);

> if (rv == -1) {
> perror("poll error");
> return -1;
> }
> if (rv == 0) {
> puts("timout error");
> return 1;
> }
> if (po[0].revents & POLLOUT) {

if (po.revents & POLLOUT) {

> send(sock, msg, strlen(msg) + 1, 0);
> puts("success");
> return 0;
> }
> return 0;
> }