ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hello everyone,
I'm trying to build a socket class that works similar to the visual basic win_sock.
Is it possible in c++ to know if a data has arrived to the socket? Like interrupt or event?
How do I know that I received something like the VB. In VB if Data is arrived the function Data_arrival is called. Can I do this in C or C++?
You can make a new thread in your program which calls the select() system call to wait for data on the socket. I vaguely recall select() being able to have a timeout so you could, rather than use a separate thread, simply periodically poll the socket for new data.
Book recommendation:
Unix Network Programming, 3rd Edition, W. Richard Stevens, Andrew M. Rudoff http://www.bookpool.com/sm/0131411551
<= *THE* BOOK ON SOCKETS; MUCH EXCELLENT DETAIL ON EVERYTHING
YOU'LL NEED/WANT TO KNOW ... INCLUDING "SELECT()"
Thanks guys. select workd perfectly, here is an example of what i did:
#include <stdio.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
int
main(void) {
fd_set rfds;
struct timeval tv;
int retval;
/* Watch stdin (fd 0) to see when it has input. */
FD_ZERO(&rfds);
FD_SET(0, &rfds);
/* Wait up to five seconds. */
tv.tv_sec = 5;
tv.tv_usec = 0;
retval = select(1, &rfds, NULL, NULL, &tv);
/* Don’t rely on the value of tv now! */
if (retval)
printf("Data is available now.\n");
/* FD_ISSET(0, &rfds) will be true. */
else
printf("No data within five seconds.\n");
return 0;
}
One design caveat - please always remember the mantra:
Polling is Bad
That is, you want to structure your "main event loop" so that it blocks - FOREVER - until input arrives. You do NOT (if you can at all help it) have a design where you periodically check whether or not anything has happened.
In your coding please always regard a timeout as a "yellow flag" and ask yourself if you can restructure things so that you DON'T need to time out and you DON'T have to incur the overhead of "checking anything".
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.