3

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?

thuovila
  • 1,960
  • 13
  • 21
Chap
  • 3,649
  • 2
  • 46
  • 84

1 Answers1

1

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 ) ;
      }
   }
thuovila
  • 1,960
  • 13
  • 21
  • That's certainly good to know. My only hesitation is that the server, as it was previously designed, was launched in a different way such that there was a true socket for *every* connection, and the data structures and algorithms just assumed that each socket was bi-directional. So, *ideally*, I would somehow be able to convert STDIN/OUT to a socket and use the existing code as it stands. (Otherwise I'll go in and special-case the STD file descriptors.) – Chap May 23 '13 at 19:31
  • Per your EDIT: I ran a test server: **my $input = ;** **log_info( $me, "Read '$input' from client.");** **print STDIN "Hi there client\n";** and a client that opened an IO::Socket, printed "hello" on it, and then tried to receive on it. The server received my hello but then raised an error printing to STDIN: **Filehandle STDIN opened only for input**. I don't know, though, whether this accurately represented your suggestion, since I don't know how to work at the FILENO level in Perl. – Chap May 24 '13 at 16:24
  • Ah, I did not realize you are using Perl. My apologies for confusing you with a c-language specific answer. – thuovila May 24 '13 at 18:09