Thread: Pipe not working fully

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    8

    Question Pipe not working fully

    I have made a little program that works similar to cat, but it also decrypts/encrypts the stream, as well as printing to stdout. It does this by simple XOR encryption.

    Code:
    /* XOR each character in the stream, printing each XOR'ed char to stdout. */
    while( (ch = getc(fp)) != EOF )
    {
    	putc(ch^(*(argv[key_arg]+offset)), stdout);
    	if( ++offset >= length ) 
    		offset = 0;
    }
    Above is a fragment of my code; it XORs a character read from a file with a "key" value, and then prints that value to stdout.
    It has a very specific problem: it won't pipe data read from stdin to other programs, it will only pipe "normal" files successfully.

    When I run a command like this:
    xor [file] [key] | nc 192.168.0.1 1234
    the listening nc will recieve the XOR encrypted [file], no hassle at all.

    But, when I run this,
    xor [key] | nc 192.168.0.1 1234
    the listening nc doesn't recieve any data that I send (entered via keyboard).

    When I test it with cat,
    cat | nc 192.168.0.1 1234
    the listening nc recieves any data that I send.

    When I just run,
    xor [key]
    It works just fine; it prints the XOR'ed value of each char that I enter via the keyboard.

    This is only a problem when I try to use stdin as input and pipe the XOR'ed output to another program. The program will pipe a "normal" file to other programs, no trouble at all.
    Why does it appear to work for stdin by itself, but not when used in conjunction with other programs?
    I have tried reading the source for cat, but it is too complex for me to understand fully.

    PS
    I am simply assigning stdin to fp like this:
    fp = stdin;
    Last edited by Heraclitus; 03-04-2003 at 06:15 PM.

  2. #2
    Registered User
    Join Date
    Feb 2003
    Posts
    8

    Red face

    Thanks for the reply man, but I have just found out what the problem was: no "fflush(stdout);". I kicked myself after realising that I overlooked this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Named pipe problem
    By rahul_c in forum C Programming
    Replies: 3
    Last Post: 10-02-2007, 05:40 PM
  2. Pipe(): Interprocess or Intraprocess comm?
    By @nthony in forum C Programming
    Replies: 2
    Last Post: 03-28-2007, 07:27 PM
  3. Clearing a pipe used in IPC
    By cbgb in forum C Programming
    Replies: 5
    Last Post: 09-26-2006, 03:59 PM
  4. Pipe problem, popen(), pipe().
    By apacz in forum C Programming
    Replies: 7
    Last Post: 06-08-2006, 12:55 AM
  5. how do i compile pipe() API on windows ?
    By intruder in forum C++ Programming
    Replies: 2
    Last Post: 01-04-2006, 06:39 PM