Thread: Peeking at streams

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    17

    Peeking at streams

    I'm trying to create a crude telnet type program (eventually evolving into an irc client) for windows, in C.

    Took me a while, but I got sockets down, but now I'm stuck on the user input aspect. Is there a way to "peek" at stdin? I'm sure there are methods out there to do this, and I have been searching the forum for about an hour now with no real luck, but I cannot seem to find them.

    In a sense, I have an infinite while loop that waits for messages from the server I'm connecting to (works fine), but also, it needs to also handle user input for commands and such (does not work). Is there any type of streamPeak() type function out there? Or is there a different, more convoluted approach I'm going to have to take?

    Thanks, and I'm sorry if this topic has already been posted. I'm not that frequent of a poster.

  2. #2
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    You should really be doing this with multithreading.

    Anyway, there's the non-standard function kbhit() which will return non-zero if the user has pressed a key. getch() will return the next byte in the buffer. getch() input is completely unformatted. Both are declared in conio.h, so your compiler must have the conio library.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    17
    Multithreading... thats what I needed. I just didn't know the term.

    Thanks!

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    17
    Any chance you know a good place to learn about windows multithreading in C?

    Thanks.

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    If you understand what you're doing, you're not learning anything.

  6. #6
    Registered User
    Join Date
    Oct 2005
    Posts
    17
    Forums would be so much better if people didn't assume everyone asking a question doesn't know about google.

    Do you really think I didn't googled it for an hour? (look at timestamps on posts).

    I got it working using MSDN documentation, but its kinda weird.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Let's see, they've been here roughly 100x longer than you have, so I suspect they know just a bit more about how people tend to post. Most people don't search for anything. They don't read the FAQ. They don't do jack ......... The fact is, you're an idiot if you can't find out how to peek at a socket. I can find out how with 3 keywords and about 30 seconds simply glancing at the first page of results.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Oct 2005
    Posts
    17
    Read this again, maybe you'll realize something:

    Is there a way to "peek" at stdin?
    Taken from my frist post. Or maybe even this:

    and I have been searching the forum for about an hour now with no real luck
    Hell why not just read the title:
    Peeking at streams
    Hmm. Amazing what you can find when you actually READ.
    Last edited by Achy; 06-09-2006 at 12:23 AM.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Speaking of reading...
    Quote Originally Posted by you, dumbass!
    and I have been searching the forum
    I hate to tell you this, but Google is not the forum.

    See, you can only get away with being a smart ass if you're actually smart, and not a complete moron.


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    Oct 2005
    Posts
    17
    Just because I never specifically said I searched google doesn't mean I didn't. Of course, that kind of assumption is probably never made by the likes of you.

    Besides, that doesn't change the fact that you totally failed to read my posts, then flamed me for no reason. Get a life man. Just because you have 7k posts doesn't give you the privilage of flaming people that might not know how to do this or that. What the HELL is the point of a forum if you can't ask questions? And why would I be a "complete idiot" if I didn't know how to peek at a socket (which I did know how to do anyway)?

    Yes, I know most of the answers I need are out there, but why not use a forum with thousands of people that know their stuff much better than I do? Why waste multiple hours searching the net, when someone might be able to give me a quick and easy (and most of the time, better) answer on a message board? Of course I try to find answers elsewhere, but if I can't find it quickly, I'm gonna post...

    Get your head out of your ass and realize that everyone here is just trying to learn more about programming. Is that a bad thing? Do you think that people should just be born ub4r l33t like you? Maybe one day you will realize that when people ask questions it doesn't mean they are complete retards (morons, idiots, etc). They just want to learn.

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Use getc() and then ungetc().
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  12. #12
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by Achy
    Just because I never specifically said I searched google doesn't mean I didn't. <snip>
    You miss the point completely. Chances are that someone asked your question here before you did, so you can search the forum and get lots of threads with answers. THEN if you are still having problems, post a thread.

  13. #13
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    For instance, if you search the C programming forum for "peek stdin", you get a thread that mentions ungetc().
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  14. #14
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Rule #1: Don't annoy the C gods.



    Note that ungetc() will only work for the first char you put back in; if you try to backtrack 158 characters, results will be undefined... scary... oooh...
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Writing to two streams: va_arg issues
    By dwks in forum C Programming
    Replies: 2
    Last Post: 09-26-2007, 10:14 AM
  2. Mulitple I/O streams
    By thamisfit in forum C Programming
    Replies: 3
    Last Post: 03-22-2003, 04:15 PM
  3. streams
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 05-02-2002, 12:04 PM
  4. Independent streams of pseudo random number generator
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 11-15-2001, 05:32 AM
  5. Replies: 4
    Last Post: 10-16-2001, 02:00 PM