Chap
Chap

Reputation: 3835

Using STDIN/STDOUT as a socket

I'm having a client process launch a server process with xinetd. As I understand it, the server come to life being able to read from the client via its STDIN, and write to the client via its STDOUT. For various reasons (mainly having the server be able to use select() and then read() or recv() ) I want to be able to somehow transform STDIN and STDOUT into a socket and then perform ordinary socket I/O on it, as I will with the other servers that my server will connect to.

Can this be done? Is this even a meaningful question?

Upvotes: 3

Views: 2620

Answers (1)

thuovila
thuovila

Reputation: 2020

All the function calls you mention can be used on the file descriptors with the numbers STDIN_FILENO and STDOUT_FILENO (from <unistd.h>). So I would venture you dont need any transformation.

EDIT: A bit unorthodox, but have you tried writing on STDOUT_FILE or reading STDIN_FILENO in your server? By very quickly glancing at the xinetd code, it looks like it just dup()s the open socket fd to all fds of the server. Maybe you can use either one as full duplex.

I only looked here

  for ( fd = 0 ; fd <= MAX_PASS_FD ; fd++ )
   {
      if ( dup2( descriptor, fd ) == -1 )
      {
     msg_resume();
         msg( LOG_ERR, func,
               "dup2( %d, %d ) failed: %m", descriptor, fd ) ;
         _exit( 1 ) ;
      }
   }

Upvotes: 1

Related Questions