dup'd socket to stdin, problem...

dup'd socket to stdin, problem...

Post by M.W. Holcro » Wed, 30 Aug 1995 04:00:00

I have a(n apparantly obscure) problem with sockets.

---- What I am doing, in outline ------------------------------------------

bind ( main_sock, (struct sockaddr*) &server, sizeof (struct sockaddr_in))
listen ( main_sock, 10 )

while ( 1 )
   request_socket = accept ( main_sock, (struct sockaddr*) &source, &i ) ;
   socketStream   = fdopen ( request_socket, "r+b" );

   fgets ( request, BUFSIZ, socketStream );
   fscanf ( socketStream, "%20s", &j );

   i = fileno ( socketStream );
   dup2   ( i, 0 );
   dup2   ( i, 1 );
   close  ( request_socket );
   fclose ( socketStream );

   if ( fork () == 0 )
      execl ( fullFilename, file + (*file == '/'), (char*)0 );


The fork'd child process will not read from stdin.  I fdopen the socket
because I want to use formated io.

Prior to having the i=fileno, and dup'ing from i, I dup'ed the request_socket.
When I put in the 'i' line, it worked for the afternoon.   That evening, it
no longer worked.  Is there anything I should be aware of that I obviously
am not :)  I am absolutely certain I changed nothing that could possibly impact
on io.

I know about having to seek after a write to avoiid undefined results, so I
tried seeking the socketSream (which seems stupid thing to do) and nothing

Thanks in advance,



1. sockets dup()ed to stdin and stdout of a process

   I am posting this for a friend of mine who doesn't have access

   Is it possible to use dup() to assign a socket to be the input
*and* the output of a process? I did this (to a process which uses the
stdio functions like gets(), etc.), in the hopes of then using
select() in another program to know when this process wants to read
and when it wants to write. The reading works fine, especially when
the other process does no input. If the other process does read()s, it
works also. But if it uses gets(), it doesn't work (it hangs, etc.).
select() also thinks the connection is writable, even though the stuff
that the other process printf()ed before it issued the gets() hasn't
been seen on the other end yet. My question is: what am I missing?
What should I know about the stdio functions when they've been dup()ed
to a socket? What is the right way to write EOF to such a process?
Will this work, in general, or do I need two separate sockets, one for
input and one for output?  Any help will be greatly appreciated.

2. Which LINUX shoul I buy?

3. shutdown() for dup'ed socket

4. Help ! e2fsck blues....

5. KDE 1.1 problem - 'stdin: is not a tty' on xconsole

6. global sed for "/ /" to "/M/"

7. dup'ing another process fd's

8. Solaris 8: user can't unlock screen

9. dup()'ed pipe()'s to stdio

10. How can I tell if a socket file descriptor is a dup() of another?

11. Socket, socket, who's got the socket?

12. Increasing throughput on server sockets via dup?

13. Failed to connect to term socket '/home2/irvine/.term/socket'