LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-24-2006, 04:30 PM   #1
allomeen
Member
 
Registered: Dec 2005
Posts: 83

Rep: Reputation: 15
Event driven connection


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++?


Thank you
alaa
 
Old 02-26-2006, 01:14 AM   #2
pcweirdo
Member
 
Registered: Oct 2004
Location: Melbourne-ish, Australia
Distribution: Gentoo, Windows XP Pro
Posts: 58

Rep: Reputation: 15
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.

Ask more questions.

-pcweirdo.
 
Old 02-26-2006, 06:52 AM   #3
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 79
Moved: This thread is more suitable in Programming and has been moved accordingly to help your thread/question get the exposure it deserves.
 
Old 02-26-2006, 12:24 PM   #4
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
pcweirdo is right: if you're on Linux, "select()" what you want to look at:
http://www.lowtek.com/sockets/select.html

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()"
 
Old 02-27-2006, 06:08 PM   #5
allomeen
Member
 
Registered: Dec 2005
Posts: 83

Original Poster
Rep: Reputation: 15
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;
}
 
Old 02-27-2006, 07:54 PM   #6
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Cool - we're glad we could help.

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".

IMHO .. PSM

Last edited by paulsm4; 02-27-2006 at 07:56 PM.
 
Old 02-28-2006, 04:42 AM   #7
pcweirdo
Member
 
Registered: Oct 2004
Location: Melbourne-ish, Australia
Distribution: Gentoo, Windows XP Pro
Posts: 58

Rep: Reputation: 15
Quote:
Originally Posted by paulsm4
pcweirdo is right
Bookmark this thread. Nobody will ever, ever say those words on this internet again.

allomeen I barely comprehend what you've done there but I salute you. Keep up the good work, and remember the code tags to preserve leading spaces.

-pcweirdo.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Connection between Touch Screen Device Driver and /dev/input/event ... okeyla Linux - Hardware 2 03-05-2006 05:52 AM
Community driven advertisment? Caboose General 4 04-10-2005 06:24 PM
Test-Driven Development question Hockeyfan Programming 4 03-03-2005 03:58 AM
Hotplug event drop my intrnet connection! Nik0s Linux - Networking 12 01-25-2004 02:11 PM
Event driven object-to-object: C++ template class mecanism ( NOT STL or STDC++) bretzeltux Programming 2 12-23-2003 02:45 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:06 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration