Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?

share|improve this question
    
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
    
Beej's Guide to Network Programming 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 3:38
    
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 4:01
    
Here is a book : Unix Network Programming. 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 4:04
    
Do you mean BSD sockets? Unix sockets only work locally. –  imel96 Dec 1 at 0:58

3 Answers 3

up vote 7 down vote accepted

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.

share|improve this answer

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.

share|improve this answer
    
Thanks but I still find it complicated and full of argot. –  Elzo Valugi Feb 20 '12 at 18:58
    
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
1  
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 5 '12 at 12:19

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.