Help on program please..

Status
Not open for further replies.

PitViper

Ars Scholae Palatinae
812
let me preface this post with the necessary info. This is not for a school assignment or homework. I am doing this as a side project at work.

Ok, if any of you are familiar with tethereal, you know that it runs indefinitely returning packet information to the output buffer. I want to write a program in which the output to tethereal is directly input into the program. Therefore my program must run indefinitely checking the stdin buffer. I have never used the stdin buffer before, so bear with me. My real question is, what is the best way to handle the buffer when the data does not come consistantly? I suppose that I just need to check the a flag or stdin to see if its null (no data) but if it does, transfer the data into my own line buffer until I get a newline in the stream. from there I pass the linebuffer to processed and stored in a data structure. I figure I want it to dump to file its current data every hour, so I must do a time test on that loop that is checking the stdin buffer. I believe there is a timediff function in the time library that could work. Would that do the job? I have the design all down, but just wanted to prod the minds of some of the expert coders on this forum for tips on efficient ways or pitfalls I need to avoid. I am writing it as a console program in visual c++. If you have any comments (no code please), thanks in advance.
 

earl

Well-known member
3,066
select will allow you to wait with a timeout for a set of streams to have input. So you would call select on stdin with a timeout (initially) of an hour. If select returns, you check the return val: if 0 you hit your timeout, if not zero then returns the # of descriptors ready to be read. So you then write your data, decrement your timeval structure the elapsed time, and repeat. Easy.

-earl-
 

angst

Smack-Fu Master, in training
51
Hmmm using Visual C++ poses a bit of a challange, popen(...) is a simple way to start a program from within a program and capture the output but I don't know if there's a VC equivalent.

_popen(...) is the equivalent of POSIX.2's popen(...) ( note the underscore ), the other interesting procedures you might want to look at are _read(...), select(...), and _fileno(...). The methology is quite simple ... open a pipe to your desired process, setup the pipe's file descriptor for reading in select(...) and wait for any activity, that's about it. It took me couple minutes to write the program in linux and another couple to port it to VC++ ( it's tiny < 30 lines including error checking )

edit: if you decide to use select(...) you must link against the ws2_32 library.

[This message was edited by angst on December 16, 2001 at 12:38.]
 
Status
Not open for further replies.