Is there a file for each socket?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP












20















"Everything is a file" in the UNIX World.



Above sentence is famous. When I run echo "hello programmer" >> /dev/tty1 , I can watch the given string on TeleType 1 , ....



What and where is file per each socket? Suppose my friend connects to my PC, and its IP is h.h.h.h , how can I access the respective file? Is it possible?










share|improve this question



















  • 3





    The socket API is actually a deviation from the "Unix policy" in this regard, because it originally came from BSD. Note that there's always Plan 9 from Bell Labs which is "more Unix than Unix" – even the network and graphics APIs are files there.

    – ntoskrnl
    Feb 23 '14 at 21:39












  • sockets and processes are mentioned a fair amount in Plan 9 papers, mostly talking about where the UNIX model went wrong.

    – strugee
    Feb 24 '14 at 1:31















20















"Everything is a file" in the UNIX World.



Above sentence is famous. When I run echo "hello programmer" >> /dev/tty1 , I can watch the given string on TeleType 1 , ....



What and where is file per each socket? Suppose my friend connects to my PC, and its IP is h.h.h.h , how can I access the respective file? Is it possible?










share|improve this question



















  • 3





    The socket API is actually a deviation from the "Unix policy" in this regard, because it originally came from BSD. Note that there's always Plan 9 from Bell Labs which is "more Unix than Unix" – even the network and graphics APIs are files there.

    – ntoskrnl
    Feb 23 '14 at 21:39












  • sockets and processes are mentioned a fair amount in Plan 9 papers, mostly talking about where the UNIX model went wrong.

    – strugee
    Feb 24 '14 at 1:31













20












20








20


10






"Everything is a file" in the UNIX World.



Above sentence is famous. When I run echo "hello programmer" >> /dev/tty1 , I can watch the given string on TeleType 1 , ....



What and where is file per each socket? Suppose my friend connects to my PC, and its IP is h.h.h.h , how can I access the respective file? Is it possible?










share|improve this question
















"Everything is a file" in the UNIX World.



Above sentence is famous. When I run echo "hello programmer" >> /dev/tty1 , I can watch the given string on TeleType 1 , ....



What and where is file per each socket? Suppose my friend connects to my PC, and its IP is h.h.h.h , how can I access the respective file? Is it possible?







files kernel socket






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 23 '14 at 16:21









Hauke Laging

56.2k1285135




56.2k1285135










asked Feb 23 '14 at 16:04









PersianGulfPersianGulf

6,93543461




6,93543461







  • 3





    The socket API is actually a deviation from the "Unix policy" in this regard, because it originally came from BSD. Note that there's always Plan 9 from Bell Labs which is "more Unix than Unix" – even the network and graphics APIs are files there.

    – ntoskrnl
    Feb 23 '14 at 21:39












  • sockets and processes are mentioned a fair amount in Plan 9 papers, mostly talking about where the UNIX model went wrong.

    – strugee
    Feb 24 '14 at 1:31












  • 3





    The socket API is actually a deviation from the "Unix policy" in this regard, because it originally came from BSD. Note that there's always Plan 9 from Bell Labs which is "more Unix than Unix" – even the network and graphics APIs are files there.

    – ntoskrnl
    Feb 23 '14 at 21:39












  • sockets and processes are mentioned a fair amount in Plan 9 papers, mostly talking about where the UNIX model went wrong.

    – strugee
    Feb 24 '14 at 1:31







3




3





The socket API is actually a deviation from the "Unix policy" in this regard, because it originally came from BSD. Note that there's always Plan 9 from Bell Labs which is "more Unix than Unix" – even the network and graphics APIs are files there.

– ntoskrnl
Feb 23 '14 at 21:39






The socket API is actually a deviation from the "Unix policy" in this regard, because it originally came from BSD. Note that there's always Plan 9 from Bell Labs which is "more Unix than Unix" – even the network and graphics APIs are files there.

– ntoskrnl
Feb 23 '14 at 21:39














sockets and processes are mentioned a fair amount in Plan 9 papers, mostly talking about where the UNIX model went wrong.

– strugee
Feb 24 '14 at 1:31





sockets and processes are mentioned a fair amount in Plan 9 papers, mostly talking about where the UNIX model went wrong.

– strugee
Feb 24 '14 at 1:31










3 Answers
3






active

oldest

votes


















9














man 7 unix:




The AF_UNIX (also known as AF_LOCAL) socket family is used to communicate between processes on the same machine efficiently. Traditionally, UNIX domain
sockets can be either unnamed, or bound to a file system pathname (marked as being of type socket). Linux also supports an abstract namespace which is
independent of the file system.




I.e. not every socket can be seen as a file (in the sense of "no file without a file name").



But there are files with lists of sockets (e.g. /proc/net/tcp); not exactly what "everything is a file" means, though.






share|improve this answer






























    37














    A socket is a file. But not all files have names. Here are a few examples of files that don't have names:



    • Any file that used to have a name, and is now deleted, but is still opened by a program.

    • An unnamed pipe, such as one created by the | shell operator.

    • Most sockets: any Internet socket, or a Unix socket which is not in the filesystem namespace (it can be in the abstract namespace or unnamed).

    Files such as unnamed pipes or sockets are created by a process and can only be accessed in that process or in subsequently-created child processes. (This is not completely true: a process that has a pipe or socket (or any other file) open can transmit it to other processes via a Unix socket; this is known as file descriptor passing.)



    Sockets that have a name (whether in the filesystem or abstract) can be opened using that name. Network sockets can be opened (or more precisely connected to) remotely from any machine that has appropriate connectivity.






    share|improve this answer

























    • This is the correct answer.

      – jforberg
      Aug 27 '15 at 23:51






    • 4





      /proc/<pid>/fd/* and /proc/net/* may be interesting

      – n611x007
      Dec 25 '15 at 3:56











    • Please accept this answer. It is IMHO a lot more accurate.

      – user1202136
      Sep 23 '16 at 15:05


















    13















    What and where is file per each socket?




    "Everything" is an exaggeration. It's not a strict policy, it's just a common practice to use the filesystem for interfaces since filesystem access is synonymous with system calls (i.e., the filesystem is really an interface with the kernel, and so provides a convenient format for all kinds of things). Other operating systems do not make as much use of this, so it is considered a distinguishing feature.



    As Hauke Laging mentions, "unix local" sockets have a file node as do named pipes (see man fifo). However, internet protocol sockets (used for network communication) do not. Instead, they are associated in userspace with a port number. Note that a server socket on a single port connects multiple clients each with their own individual socket (a single unix local socket file can be also be used this way with a server, meaning, there may be multiple sockets associated with the same file address) and in code they are in fact identified individually via separate numerical file descriptors.



    So, in that sense all sockets are much like files, and have a link in /proc/[pid]/fd/. You can even call readlink() on this inode and get a special sort of filename, which is used in command line tools such as lsof, I believe; likewise you can get information about the socket descriptor via fstat().






    share|improve this answer

























    • You mean "identified in userspace by their inode"? Not every socket has a port number and there can be several sockets for the same port number (doesn't make sense, though).

      – Hauke Laging
      Feb 23 '14 at 17:29











    • @HaukeLaging : Good point. I've edited to make this clearer starting with the second paragraph.

      – goldilocks
      Feb 24 '14 at 15:51










    Your Answer








    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f116563%2fis-there-a-file-for-each-socket%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    9














    man 7 unix:




    The AF_UNIX (also known as AF_LOCAL) socket family is used to communicate between processes on the same machine efficiently. Traditionally, UNIX domain
    sockets can be either unnamed, or bound to a file system pathname (marked as being of type socket). Linux also supports an abstract namespace which is
    independent of the file system.




    I.e. not every socket can be seen as a file (in the sense of "no file without a file name").



    But there are files with lists of sockets (e.g. /proc/net/tcp); not exactly what "everything is a file" means, though.






    share|improve this answer



























      9














      man 7 unix:




      The AF_UNIX (also known as AF_LOCAL) socket family is used to communicate between processes on the same machine efficiently. Traditionally, UNIX domain
      sockets can be either unnamed, or bound to a file system pathname (marked as being of type socket). Linux also supports an abstract namespace which is
      independent of the file system.




      I.e. not every socket can be seen as a file (in the sense of "no file without a file name").



      But there are files with lists of sockets (e.g. /proc/net/tcp); not exactly what "everything is a file" means, though.






      share|improve this answer

























        9












        9








        9







        man 7 unix:




        The AF_UNIX (also known as AF_LOCAL) socket family is used to communicate between processes on the same machine efficiently. Traditionally, UNIX domain
        sockets can be either unnamed, or bound to a file system pathname (marked as being of type socket). Linux also supports an abstract namespace which is
        independent of the file system.




        I.e. not every socket can be seen as a file (in the sense of "no file without a file name").



        But there are files with lists of sockets (e.g. /proc/net/tcp); not exactly what "everything is a file" means, though.






        share|improve this answer













        man 7 unix:




        The AF_UNIX (also known as AF_LOCAL) socket family is used to communicate between processes on the same machine efficiently. Traditionally, UNIX domain
        sockets can be either unnamed, or bound to a file system pathname (marked as being of type socket). Linux also supports an abstract namespace which is
        independent of the file system.




        I.e. not every socket can be seen as a file (in the sense of "no file without a file name").



        But there are files with lists of sockets (e.g. /proc/net/tcp); not exactly what "everything is a file" means, though.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Feb 23 '14 at 16:18









        Hauke LagingHauke Laging

        56.2k1285135




        56.2k1285135























            37














            A socket is a file. But not all files have names. Here are a few examples of files that don't have names:



            • Any file that used to have a name, and is now deleted, but is still opened by a program.

            • An unnamed pipe, such as one created by the | shell operator.

            • Most sockets: any Internet socket, or a Unix socket which is not in the filesystem namespace (it can be in the abstract namespace or unnamed).

            Files such as unnamed pipes or sockets are created by a process and can only be accessed in that process or in subsequently-created child processes. (This is not completely true: a process that has a pipe or socket (or any other file) open can transmit it to other processes via a Unix socket; this is known as file descriptor passing.)



            Sockets that have a name (whether in the filesystem or abstract) can be opened using that name. Network sockets can be opened (or more precisely connected to) remotely from any machine that has appropriate connectivity.






            share|improve this answer

























            • This is the correct answer.

              – jforberg
              Aug 27 '15 at 23:51






            • 4





              /proc/<pid>/fd/* and /proc/net/* may be interesting

              – n611x007
              Dec 25 '15 at 3:56











            • Please accept this answer. It is IMHO a lot more accurate.

              – user1202136
              Sep 23 '16 at 15:05















            37














            A socket is a file. But not all files have names. Here are a few examples of files that don't have names:



            • Any file that used to have a name, and is now deleted, but is still opened by a program.

            • An unnamed pipe, such as one created by the | shell operator.

            • Most sockets: any Internet socket, or a Unix socket which is not in the filesystem namespace (it can be in the abstract namespace or unnamed).

            Files such as unnamed pipes or sockets are created by a process and can only be accessed in that process or in subsequently-created child processes. (This is not completely true: a process that has a pipe or socket (or any other file) open can transmit it to other processes via a Unix socket; this is known as file descriptor passing.)



            Sockets that have a name (whether in the filesystem or abstract) can be opened using that name. Network sockets can be opened (or more precisely connected to) remotely from any machine that has appropriate connectivity.






            share|improve this answer

























            • This is the correct answer.

              – jforberg
              Aug 27 '15 at 23:51






            • 4





              /proc/<pid>/fd/* and /proc/net/* may be interesting

              – n611x007
              Dec 25 '15 at 3:56











            • Please accept this answer. It is IMHO a lot more accurate.

              – user1202136
              Sep 23 '16 at 15:05













            37












            37








            37







            A socket is a file. But not all files have names. Here are a few examples of files that don't have names:



            • Any file that used to have a name, and is now deleted, but is still opened by a program.

            • An unnamed pipe, such as one created by the | shell operator.

            • Most sockets: any Internet socket, or a Unix socket which is not in the filesystem namespace (it can be in the abstract namespace or unnamed).

            Files such as unnamed pipes or sockets are created by a process and can only be accessed in that process or in subsequently-created child processes. (This is not completely true: a process that has a pipe or socket (or any other file) open can transmit it to other processes via a Unix socket; this is known as file descriptor passing.)



            Sockets that have a name (whether in the filesystem or abstract) can be opened using that name. Network sockets can be opened (or more precisely connected to) remotely from any machine that has appropriate connectivity.






            share|improve this answer















            A socket is a file. But not all files have names. Here are a few examples of files that don't have names:



            • Any file that used to have a name, and is now deleted, but is still opened by a program.

            • An unnamed pipe, such as one created by the | shell operator.

            • Most sockets: any Internet socket, or a Unix socket which is not in the filesystem namespace (it can be in the abstract namespace or unnamed).

            Files such as unnamed pipes or sockets are created by a process and can only be accessed in that process or in subsequently-created child processes. (This is not completely true: a process that has a pipe or socket (or any other file) open can transmit it to other processes via a Unix socket; this is known as file descriptor passing.)



            Sockets that have a name (whether in the filesystem or abstract) can be opened using that name. Network sockets can be opened (or more precisely connected to) remotely from any machine that has appropriate connectivity.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Jan 6 at 4:24









            heemayl

            34.8k373103




            34.8k373103










            answered Feb 24 '14 at 1:08









            GillesGilles

            532k12810661593




            532k12810661593












            • This is the correct answer.

              – jforberg
              Aug 27 '15 at 23:51






            • 4





              /proc/<pid>/fd/* and /proc/net/* may be interesting

              – n611x007
              Dec 25 '15 at 3:56











            • Please accept this answer. It is IMHO a lot more accurate.

              – user1202136
              Sep 23 '16 at 15:05

















            • This is the correct answer.

              – jforberg
              Aug 27 '15 at 23:51






            • 4





              /proc/<pid>/fd/* and /proc/net/* may be interesting

              – n611x007
              Dec 25 '15 at 3:56











            • Please accept this answer. It is IMHO a lot more accurate.

              – user1202136
              Sep 23 '16 at 15:05
















            This is the correct answer.

            – jforberg
            Aug 27 '15 at 23:51





            This is the correct answer.

            – jforberg
            Aug 27 '15 at 23:51




            4




            4





            /proc/<pid>/fd/* and /proc/net/* may be interesting

            – n611x007
            Dec 25 '15 at 3:56





            /proc/<pid>/fd/* and /proc/net/* may be interesting

            – n611x007
            Dec 25 '15 at 3:56













            Please accept this answer. It is IMHO a lot more accurate.

            – user1202136
            Sep 23 '16 at 15:05





            Please accept this answer. It is IMHO a lot more accurate.

            – user1202136
            Sep 23 '16 at 15:05











            13















            What and where is file per each socket?




            "Everything" is an exaggeration. It's not a strict policy, it's just a common practice to use the filesystem for interfaces since filesystem access is synonymous with system calls (i.e., the filesystem is really an interface with the kernel, and so provides a convenient format for all kinds of things). Other operating systems do not make as much use of this, so it is considered a distinguishing feature.



            As Hauke Laging mentions, "unix local" sockets have a file node as do named pipes (see man fifo). However, internet protocol sockets (used for network communication) do not. Instead, they are associated in userspace with a port number. Note that a server socket on a single port connects multiple clients each with their own individual socket (a single unix local socket file can be also be used this way with a server, meaning, there may be multiple sockets associated with the same file address) and in code they are in fact identified individually via separate numerical file descriptors.



            So, in that sense all sockets are much like files, and have a link in /proc/[pid]/fd/. You can even call readlink() on this inode and get a special sort of filename, which is used in command line tools such as lsof, I believe; likewise you can get information about the socket descriptor via fstat().






            share|improve this answer

























            • You mean "identified in userspace by their inode"? Not every socket has a port number and there can be several sockets for the same port number (doesn't make sense, though).

              – Hauke Laging
              Feb 23 '14 at 17:29











            • @HaukeLaging : Good point. I've edited to make this clearer starting with the second paragraph.

              – goldilocks
              Feb 24 '14 at 15:51















            13















            What and where is file per each socket?




            "Everything" is an exaggeration. It's not a strict policy, it's just a common practice to use the filesystem for interfaces since filesystem access is synonymous with system calls (i.e., the filesystem is really an interface with the kernel, and so provides a convenient format for all kinds of things). Other operating systems do not make as much use of this, so it is considered a distinguishing feature.



            As Hauke Laging mentions, "unix local" sockets have a file node as do named pipes (see man fifo). However, internet protocol sockets (used for network communication) do not. Instead, they are associated in userspace with a port number. Note that a server socket on a single port connects multiple clients each with their own individual socket (a single unix local socket file can be also be used this way with a server, meaning, there may be multiple sockets associated with the same file address) and in code they are in fact identified individually via separate numerical file descriptors.



            So, in that sense all sockets are much like files, and have a link in /proc/[pid]/fd/. You can even call readlink() on this inode and get a special sort of filename, which is used in command line tools such as lsof, I believe; likewise you can get information about the socket descriptor via fstat().






            share|improve this answer

























            • You mean "identified in userspace by their inode"? Not every socket has a port number and there can be several sockets for the same port number (doesn't make sense, though).

              – Hauke Laging
              Feb 23 '14 at 17:29











            • @HaukeLaging : Good point. I've edited to make this clearer starting with the second paragraph.

              – goldilocks
              Feb 24 '14 at 15:51













            13












            13








            13








            What and where is file per each socket?




            "Everything" is an exaggeration. It's not a strict policy, it's just a common practice to use the filesystem for interfaces since filesystem access is synonymous with system calls (i.e., the filesystem is really an interface with the kernel, and so provides a convenient format for all kinds of things). Other operating systems do not make as much use of this, so it is considered a distinguishing feature.



            As Hauke Laging mentions, "unix local" sockets have a file node as do named pipes (see man fifo). However, internet protocol sockets (used for network communication) do not. Instead, they are associated in userspace with a port number. Note that a server socket on a single port connects multiple clients each with their own individual socket (a single unix local socket file can be also be used this way with a server, meaning, there may be multiple sockets associated with the same file address) and in code they are in fact identified individually via separate numerical file descriptors.



            So, in that sense all sockets are much like files, and have a link in /proc/[pid]/fd/. You can even call readlink() on this inode and get a special sort of filename, which is used in command line tools such as lsof, I believe; likewise you can get information about the socket descriptor via fstat().






            share|improve this answer
















            What and where is file per each socket?




            "Everything" is an exaggeration. It's not a strict policy, it's just a common practice to use the filesystem for interfaces since filesystem access is synonymous with system calls (i.e., the filesystem is really an interface with the kernel, and so provides a convenient format for all kinds of things). Other operating systems do not make as much use of this, so it is considered a distinguishing feature.



            As Hauke Laging mentions, "unix local" sockets have a file node as do named pipes (see man fifo). However, internet protocol sockets (used for network communication) do not. Instead, they are associated in userspace with a port number. Note that a server socket on a single port connects multiple clients each with their own individual socket (a single unix local socket file can be also be used this way with a server, meaning, there may be multiple sockets associated with the same file address) and in code they are in fact identified individually via separate numerical file descriptors.



            So, in that sense all sockets are much like files, and have a link in /proc/[pid]/fd/. You can even call readlink() on this inode and get a special sort of filename, which is used in command line tools such as lsof, I believe; likewise you can get information about the socket descriptor via fstat().







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Feb 24 '14 at 15:50

























            answered Feb 23 '14 at 16:22









            goldilocksgoldilocks

            61.8k13151208




            61.8k13151208












            • You mean "identified in userspace by their inode"? Not every socket has a port number and there can be several sockets for the same port number (doesn't make sense, though).

              – Hauke Laging
              Feb 23 '14 at 17:29











            • @HaukeLaging : Good point. I've edited to make this clearer starting with the second paragraph.

              – goldilocks
              Feb 24 '14 at 15:51

















            • You mean "identified in userspace by their inode"? Not every socket has a port number and there can be several sockets for the same port number (doesn't make sense, though).

              – Hauke Laging
              Feb 23 '14 at 17:29











            • @HaukeLaging : Good point. I've edited to make this clearer starting with the second paragraph.

              – goldilocks
              Feb 24 '14 at 15:51
















            You mean "identified in userspace by their inode"? Not every socket has a port number and there can be several sockets for the same port number (doesn't make sense, though).

            – Hauke Laging
            Feb 23 '14 at 17:29





            You mean "identified in userspace by their inode"? Not every socket has a port number and there can be several sockets for the same port number (doesn't make sense, though).

            – Hauke Laging
            Feb 23 '14 at 17:29













            @HaukeLaging : Good point. I've edited to make this clearer starting with the second paragraph.

            – goldilocks
            Feb 24 '14 at 15:51





            @HaukeLaging : Good point. I've edited to make this clearer starting with the second paragraph.

            – goldilocks
            Feb 24 '14 at 15:51

















            draft saved

            draft discarded
















































            Thanks for contributing an answer to Unix & Linux Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f116563%2fis-there-a-file-for-each-socket%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown






            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Displaying single band from multi-band raster using QGIS

            How many registers does an x86_64 CPU actually have?