View Full Version : File descriptors vs ports

12-01-2006, 11:20 AM
This is probably a painfully basic question, but these are usually the hard ones to google for. I know on linux that STDIN has a file descriptor of 0, but does that bear any relevance to the port number? Port is machine-specific, whereas file descriptors are program specific, aren't they? Don't ask me how I've managed anything without knowing this.

12-01-2006, 11:30 AM
What port are you talking about?
- serial
- parallel
- network

It doesn't make a bean of difference to Linux, everything gets returned to you as a file descriptor, which supports open / close / read / write / ioctl

12-01-2006, 12:10 PM
Sorry, I meant network port. I'm aware that file descriptors map back to anything else, but what about the other way around? What does mean? It's port 0 on the loopback, but is that stdin for the local machine, or is it just silly, nonsense, best-not-think-about-that-ness?

I've just started using [UR=http://asio.sourceforge.net/]asio[/URL] and it deals with tcp::endpoints, which deals with port numbers. No doubt there's another way but I haven't figured it out yet. It got me wondering about this though. I hope I'm not being too vague.

12-01-2006, 12:35 PM
File descriptors and net ports bear no relationship whatsoever.

12-01-2006, 01:18 PM
> What does mean? It's port 0 on the loopback
Yes, and would be what you would use for a local HTTP server.

But in your code, you would do the listen/accept thing and get a file descriptor back from the socket-level API. This would be some random small integer depending on how many other file descriptors you had open at the time.

Then you can go crazy, close say stdout and 'dup' the descriptor you got from accept() so that stdout from the process gets sent to someones web browser.

12-01-2006, 04:20 PM
Well, that nearly is what CGI does.

jim mcnamara
12-01-2006, 08:40 PM
File descriptors are process-specific.

Ports are assigned via sockets which use unique-to-the-process file descriptors - small integers usually larger than 2. The association between an actual socket's fd number and a port is pretty much random. You cannot know ahead of time what the fd number for a socket is. It definitely doesn't have anything to do with a port number the socket is associated with.

12-01-2006, 11:14 PM
Ok, thanks all for clearing that up. I found this (http://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers), which explains a lot. I was just trying to figure a way of getting boost.asio to accept from STDIN. I still haven't got it, but I'll keep looking. Cheers.