File descriptors vs ports

This is a discussion on File descriptors vs ports within the Linux Programming forums, part of the Platform Specific Boards category; This is probably a painfully basic question, but these are usually the hard ones to google for. I know on ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    88

    File descriptors vs ports

    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.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,506
    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
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    88
    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 127.0.0.1:0 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.

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    File descriptors and net ports bear no relationship whatsoever.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,506
    > What does 127.0.0.1:0 mean? It's port 0 on the loopback
    Yes, and 127.0.0.1:80 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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Well, that nearly is what CGI does.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    .
    Join Date
    Nov 2003
    Posts
    307
    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.

  8. #8
    Registered User
    Join Date
    Oct 2005
    Posts
    88
    Ok, thanks all for clearing that up. I found this, 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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  3. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  4. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  5. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 09:57 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21