Operating System - HP-UX
1821584 Members
3393 Online
109633 Solutions
New Discussion юеВ

file descriptor of socket

 
Toyomi Sakamoto
New Member

file descriptor of socket

Hi,

File descriptor from socket is always 1. It indicate the standard output.
After called socket and bind (socket fd,...)
listen will receive many stdout messages and not working well.
So,is there any solution to get another file descriptor without 1 from socket?

I'm using HP-UX 11.0

Toyomi
3 REPLIES 3
Olav Baadsvik
Esteemed Contributor

Re: file descriptor of socket


Hello,

I think you must have some bug in you code.
A call to socket will return the next
free file-descriptor and this should never be 1, at least not in a c-program.

Regards
Olav
Adam J Markiewicz
Trusted Contributor

Re: file descriptor of socket

Hi

Olav is quite right, but...
socket() (and open() also) returns always the lowest possible descriptor (that is not being currently open).

So, if earlier in your code you closed fileno=1 it is first to be reused, as it means that connection with stdout was shut for good. Moreover because of this specification the system is oblidged to return 1, as it is the lowest possible descriptor.
The optimistic conclusion is that everything should work properly with descriptor 1, as it doesn't matter for socket.
But remember that all functions that write to stdout use fileno=1 hardcoded, so you MUST NOT call any of such, as it will write to your socket.

So if you aware of such situation you have two possibilities:
1. Keep fileno=1 open. Either do not close it or open it afterwards.
2. Use dup() function to use another fileno and close original fileno=1 (However I've never tried this on sockets and manuals do not say a word about it. I guess you should do it just after socket(), before bind(), if it works at all).

However if you have stdout closed I assume your process daemonized and, as a daemon, it shouldn't perform any writing to stdout.

Good luck
Adam
I do everything perfectly, except from my mistakes
Jean-Louis Phelix
Honored Contributor

Re: file descriptor of socket

Hi,

I think that Adam is really right, but rather than dup'ing the socket file descriptor, I would add two dummy 'open' (open /dev/null for example) to be sure that you won't use stdout and stderr also and finally get a file descriptor greater than 2.

Regards.
It works for me (┬й Bill McNAMARA ...)