473,799 Members | 3,422 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

using select to poll cin

1 New Member
Hi,
I would like to poll stdin for input so I can get user input from the command line without blocking. Looking through the archives, it appears that the best way to do this is using a combination of select and cin.rdbuf()->in_avail(). However, I can't seem to get it to work properly. My select statement seems to always return true, and the cin.rdbuf()->in_avail() always returns false, regardless of if the user has typed or not. Here is the relevant code snippet, anyone have any thoughts? (I am using c++ in .net on a windows box if that helps.)

Expand|Select|Wrap|Line Numbers
  1. fd_set readfds;
  2. FD_ZERO(&readfds);
  3. FD_SET(fileno(stdin), &readfds);
  4.  
  5. struct timeval tv;
  6. tv.tv_sec = 0;
  7. tv.tv_usec = 0;
  8.  
  9.  
  10. while(true)
  11. {
  12.     select(16, &readfds, 0, 0, &tv);
  13.     //if something is available on cin, read in a char
  14.     if(FD_ISSET( fileno(stdin), &readfds ) != 0)
  15.     {
  16.         //process all available input, once character at a time
  17.         while(cin.rdbuf()->in_avail()>0)
  18.         { 
  19.             char c;
  20.         cin.get(c);
  21.             processUserInput(c);
  22.     }
  23. }
  24.  
Thanks!
Dec 5 '06 #1
0 3611

Sign in to post your reply or Sign up for a free account.

Similar topics

3
2496
by: rh0dium | last post by:
Hi all, Another newbie question. So you can't use signals on threads but you can use select. The reason I want to do this in the first place it I need a timeout. Fundamentally I want to run a command on another machine, but I need a timeout. I have to do this to a LOT of machines ( > 3000 ) and threading becomes necessary for timeliess. So I created a function which works with signals ( until you throw threading at it.. ) but I...
1
2583
by: Magnus Lycka | last post by:
I'm trying to read standard out in a process started with popen2 in a non-blocking way. (Other good ways of doing this than the one I tried are appreciated.) I've tried to dumb down my code to see what happens, and socket.poll seems to behave very strangely. I've tried to use the .poll method for the poll object with and without a timeout, but in either case, the output randomly switches between on of the versions below. It runs fast,...
1
1524
by: John Pote | last post by:
Hi all, I have the standard Python 2.4.4 windows download installed on my win XP box. But the poll class in module select is not available. The docs indicate that it can work with windows as long as only sockets are used which is fine for me. Is there a reason it's not in the standard build, can I use it if I re compile the 'select' module from the source?
0
1905
by: Maxim Veksler | last post by:
Hi, I'm trying to write a non blocking socket port listener based on poll() because select is limited to 1024 fd. Here is the code, it never gets to "I did not block" until I do a telnet connection to port 10000. """ #!/usr/bin/env python
10
1963
by: Michele Simionato | last post by:
I have always been curious about how people implement mainloops (or, in Twisted terminology, reactors). So I sit down and I wrote the following simple implementation: import itertools class SimpleReactor(object): DELAY = 0.001 # seconds
0
1612
by: Sophie000 | last post by:
I have some questions about select() and poll(). I heard the poll( ... ) call is usually preferred over and more efficient than select( ... ) simply because poll( ... ) only requires that you specify the handles you are interested in and can be quickly iterated thru by the system function call... But if you have a VERY busy server and almost ALL handles are being used - then their efficiency should be about equivalent. and in a very few cases...
2
3069
by: inhahe | last post by:
select.poll isn't supported on Windows, because Windows doesn't have such a feature, or at least it didn't until Vista. Vista implements the same thing but called WSAPoll, an article is here http://blogs.msdn.com/wndp/archive/2006/10/26/WSAPoll.aspx I hope that the next edition of Python supports select.poll on Vista, or at least that someone writes a third-party module fo it. As much as I'd love to do it myself, it's probably beyond...
1
2412
by: Jean-Paul Calderone | last post by:
On Tue, 6 May 2008 08:36:28 -0400, inhahe <inhahe@gmail.comwrote: If you use Twisted, then you can use I/O Completion Ports, which are even better than WSAPoll, and your code will also work with KQueue on BSD or EPoll on Linux without any changes. :) Jean-Paul
1
3046
by: shapper | last post by:
Hello, I have two tables: Polls PollID, Question Options OptionID, Answer Given a OptionID I want to get the poll to which the Option "Belongs": Poll poll = database.Polls.Select(p =p.Options.Select(o => o.OptionID = id)).SingleOrDefault;
0
9688
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9544
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10259
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10238
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7570
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6809
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5467
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4145
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3761
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.