21

I can search and I've already looked at the wikipedia entry and in some other places, but even though I have some programming experience, I don't really grasp the concept. Can you treat me like a 10 year old and give me a clear explanation on socket communication?

Jawwad
  • 103
  • 1
Elzo Valugi
  • 308
  • 1
  • 2
  • 9
  • Write a few small test apps using them. You don't even need two machines, just two little apps to start. Bingo. – Patrick Hughes Feb 20 '12 at 17:51
  • I already upvoted @DanielPittman answer, if i would have to write i would have written the same answer; Yet, if you want more help, i would extend. Please edit your question, put some background on what you know and what you don't and also, let me know whether you are looking at help in how to use, how they work inside, or theoretical (protocol design) or you are trying to evaluate for some purpose? – Dipan Mehta Feb 21 '12 at 04:01
  • Here is a book : [Unix Network Programming](http://www.kohala.com/start/unpv12e.html). Anyone on earth who would answer this question, would have learn from this mother. Read this book, your question itself will become better. – Dipan Mehta Feb 21 '12 at 04:04
  • [Beej's Guide to Network Programming](http://beej.us/guide/bgnet/) seems to be a very popular intro text to Unix sockets, although I can't say I like it. – Nemanja Trifunovic Feb 21 '12 at 03:38
  • Do you mean BSD sockets? Unix sockets only work locally. – imel96 Dec 01 '14 at 00:58

3 Answers3

23

Unix sockets are a bidirectional socket - just like an IP based socket, which you are probably familiar with, and kind of similar to a pipe, which you are probably familiar with.

They have a small set of interesting properties:

  • They are in the domain of "the local host" only - you can't access them over the network, only on the local machine.
  • You can create them in "stream" mode, where they just pass data like a pipe as a stream of bytes.
  • You can create them in "datagram" mode, where they retain boundaries between send operations. This allows you to retain framing without building your own framing protocol on top of a byte stream.
  • They use the filesystem, or on Linux, an "abstract namespace", as their "address"
  • You may be able to impersonate the other end, securely identify the connecting software, or pass file handles over the socket, depending on your OS.

Essentially, they are the equivalent of any other socket - they have slightly more interesting properties than pipes, but are not radically different otherwise. They do typically have higher IPC latency than a pipe does, and often larger buffers - though you may be able to tune that, and it depends on the platform.

The final interesting property to remember is that they use the filesystem as their namespace - so are like a named pipe, rather than an anonymous pipe, in that software with no previous relationship can communicate. (Abstract namespace sockets are the same, but the "file" path doesn't have to exist.)

There isn't anything deeper than that - they don't have any super-secret hidden property that makes them radically different from a typical pipe, or a TCP connection to localhost.

Daniel Pittman
  • 3,660
  • 21
  • 19
  • 3
    Thanks but I still find it complicated and full of argot. – Elzo Valugi Feb 20 '12 at 18:58
  • 2
    Please don't be offended by this try at a one line answer, to see if that better helps: "You know how TCP/IP and UDP/IP sockets work? These are the same, only without the IP part." – Daniel Pittman Feb 20 '12 at 19:02
  • I am not offended, I just want to understand. Thanks. I'll study those more in detail. – Elzo Valugi Feb 20 '12 at 19:09
  • 5
    Defining a term by using that term (ie: "Unix sockets are a bidirectional socket") doesn't explain anything to someone who doesn't know what a socket is to begin with. – Bryan Oakley Mar 05 '12 at 12:19
15

Let me give you an example: Say you want to communicate/chat with your friend, who lives not at your address. For that to happen, you have to establish a "communication channel". Say, you want to do this communication using telephones. You know that there is a network of telephone lines in the city that is extended to every house.

Now, there is a telephone socket in your house, and one in your friends house. For the communication to take place, you and your friend have to connect to the network by plugging your phone to the socket, at the both end of the communication. Sockets in programming is the same, conceptually, as the telephone sockets.

In programming, you have two processes (running programs) that want to communicate with each other. For that, they have to establish a communication link between themselves. Again, there is a network available, they just need to connect to this network using some sort of sockets. Unix sockets are one such socket that provides this connectivity/pluggability to the network. So, in each of the two programs, you will have some piece of code that does the job of connecting to the network through sockets.

The rest are details.

Nazar Merza
  • 406
  • 2
  • 5
  • 3
    I would downvote this if I had enough reputation. Your text doesn't explain how the socket works. Just the same general text as in Wikipedia. How does the socket _listen_? How your telephone socket can _listen_ for connection? – Green Apr 09 '18 at 21:37
1

Programming Linux sockets, Part 1: Using TCP/IP worked quite well for me. It starts with an introduction to IP networks and network layers, then goes on by showing how to implement a simple echo server and client in both C and Python.

Oliver Weiler
  • 2,448
  • 19
  • 28