Discussion:
[zeromq-dev] zmq_poll and stdin in C
Bekritsky, Benjamin
2016-12-13 15:35:39 UTC
Permalink
I am trying to use zmq_poll on stdin, but find that if something comes in on stdin, the poll returns with a 1, but the .revents field is zero. See code below.

Any subsequent calls to zmq_poll, immediately return with a 1, but .revents field remains 0.

Am I setting up the zmq_pollitem_t properly.

Thanks,
Benjy

int main (void)
{
void *context = zmq_ctx_new ();

int rc;
string request;
while (1)
{
zmq_pollitem_t items [1];
items[0].socket = NULL;
items[0].fd = STDIN_FILENO;
items[0].events = ZMQ_POLLIN;
/* Poll for events indefinitely */
rc = zmq_poll (items, 1, -1);

if (rc < 0)
{
perror("zmq_poll error: ");
return 0;
}
else if (rc ==0)
{
cout << "timeout" << endl;
}
else
{
//cout << rc << endl;
//cout << "items[0].socket: " << items[0].socket << " items[0].fd: " <<items[0].fd << " items[0].events: " << items[0].events << " items[0].revents: " <<items[0].revents << endl;
//cout << "items[1].socket: " << items[1].socket << " items[1].fd: " <<items[1].fd << " items[1].events: " << items[1].events << " items[1].revents: " <<items[1].revents << endl;

}


if (items[0].revents & ZMQ_POLLIN)
{
// Got a request from cli
getline(cin, request);
cout << request << endl;

}
}
zmq_ctx_destroy (context);
return 0;
}



________________________________
- CONFIDENTIAL-

This email and any files transmitted with it are confidential, and may also be legally privileged. If you are not the intended recipient, you may not review, use, copy, or distribute this message. If you receive this email in error, please notify the sender immediately by reply email and then delete this email.

________________________________
- CONFIDENTIAL-
This email and any files transmitted with it are confidential, and may also be legally privileged. If you are not the intended recipient, you may not review, use, copy, or distribute this message. If you receive this email in error, please notify the sender immediately by reply email and then delete this email.
Bekritsky, Benjamin
2016-12-15 14:47:18 UTC
Permalink
I have tested my system (Linux Ubuntu 14.04) and am able to poll for the cli using the native poll command().
I am using ZMQ version 4.2.0.

Here is my code for ZMQ:

#include <zmq.h>
#include <string>
#include <stdio.h>
#include <unistd.h>
#include <string>
#include <iostream>
using namespace std;

int main (void)
{
cout << "ZMQ CLI Poll Test" << endl;
int major, minor, patch;
zmq_version (&major, &minor, &patch);
cout << "Current ØMQ version is " << major << "." << minor << "." << patch << endl;

zmq_pollitem_t items [1];
int rc;
string request;

while (1)
{
items[0].socket = NULL;
items[0].fd = STDIN_FILENO;
items[0].events = ZMQ_POLLIN;
rc = zmq_poll (items, 1, 1000);
if (rc < 0)
{
perror("zmq_poll error");
return 0;
}

if (rc == 0)
continue;

cout << "got something" << endl;
if (items[0].revents & ZMQ_POLLIN)
{
// Got a request from cli
getline(cin, request);
cout << request << endl;
}
}

return 0;
}

And the very similar code for a standard poll:
#include <poll.h>
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <iostream>
using namespace std;

int main(void)
{
cout << "CLI Poll Test" << endl;

struct pollfd items[1];
int rc;
string request;

while (1)
{
items[0].fd = STDIN_FILENO;
items[0].events = POLLIN;
rc = poll (items, 1, 1000);

if (rc < 0)
{
perror("poll error");
return 0;
}

if (rc == 0)
continue;

cout << "got something" << endl;
if (items[0].revents & POLLIN)
{
// Got a request from cli
getline(cin, request);
cout << request << endl;

}

}

return 0;
}
Any help would be appreciated.

Thanks,
Benjy
From: zeromq-dev [mailto:zeromq-dev-***@lists.zeromq.org] On Behalf Of Bekritsky, Benjamin
Sent: Tuesday, December 13, 2016 5:36 PM
To: zeromq-***@lists.zeromq.org
Subject: [zeromq-dev] zmq_poll and stdin in C


This sender failed our fraud detection checks and may not be who they appear to be. Learn about spoofing<http://aka.ms/LearnAboutSpoofing>

Feedback<http://aka.ms/SafetyTipsFeedback>

I am trying to use zmq_poll on stdin, but find that if something comes in on stdin, the poll returns with a 1, but the .revents field is zero. See code below.

Any subsequent calls to zmq_poll, immediately return with a 1, but .revents field remains 0.

Am I setting up the zmq_pollitem_t properly.

Thanks,
Benjy

int main (void)
{
void *context = zmq_ctx_new ();

int rc;
string request;
while (1)
{
zmq_pollitem_t items [1];
items[0].socket = NULL;
items[0].fd = STDIN_FILENO;
items[0].events = ZMQ_POLLIN;
/* Poll for events indefinitely */
rc = zmq_poll (items, 1, -1);

if (rc < 0)
{
perror("zmq_poll error: ");
return 0;
}
else if (rc ==0)
{
cout << "timeout" << endl;
}
else
{
//cout << rc << endl;
//cout << "items[0].socket: " << items[0].socket << " items[0].fd: " <<items[0].fd << " items[0].events: " << items[0].events << " items[0].revents: " <<items[0].revents << endl;
//cout << "items[1].socket: " << items[1].socket << " items[1].fd: " <<items[1].fd << " items[1].events: " << items[1].events << " items[1].revents: " <<items[1].revents << endl;

}


if (items[0].revents & ZMQ_POLLIN)
{
// Got a request from cli
getline(cin, request);
cout << request << endl;

}
}
zmq_ctx_destroy (context);
return 0;
}


________________________________
- CONFIDENTIAL-

This email and any files transmitted with it are confidential, and may also be legally privileged. If you are not the intended recipient, you may not review, use, copy, or distribute this message. If you receive this email in error, please notify the sender immediately by reply email and then delete this email.
________________________________
- CONFIDENTIAL-
This email and any files transmitted with it are confidential, and may also be legally privileged. If you are not the intended recipient, you may not review, use, copy, or distribute this message. If you receive this email in error, please notify the sender immediately by reply email and then delete this email.
Loading...