In the Computer Science world, especially in networking, socket play a great role. Socket is an interface for our computer programs in order to communicate each other in this vast world. Simply it is a way to “speak” using standard Unix file descriptor. I said standard unix descriptor and yes it is basically same like file handling like create, open, write, read but with some more capabilities.
“Everything in Unix is a file!”
You may have heard those statement, well at least you have read a minute ago in this article. And yes, everything in Unix is a file. Unix do any sort of I/O by reading or writing to a file descriptor. A file descriptor is simply an integer associated with an open file. A file is Unix can be anything. It can be real file on disk, a network connection, a FIFO, a pipe, a terminal, and so on. So when you want to connect to another program over Internet you will do it through a file descriptor.
There are many kinds of sockets. There are DARPA Internet address (Internet Sockets), path names on a local node (Unix Sockets), CCITT X.25 (X.25 Sockets), and probably many others depending on which Unix subsystem. The most common is Internet Sockets as it is used for internet nowaday.
There are two commons type of internet sockets: SOCK_STREAM (stream socket) and SOCK_DGRAM (datagram socket).The types refer to connection state of them. SOCK_STREAM Is connection oriented socket while SOCK_DGRAM is a connectionless socket. They are differ from the behaviour the transmission they have to do.
A connection oriented is like its name, intend to be reliable using connection. Every packet transmitted using this socket has to be acknowledged. A receiver must send a message saying “Hey, I have receive your message” to sender so that sender know that its message has arrived at receiver end. While in the connectionless stream, a sender just send the data without requiring acknowledged message from receiver.
A connection oriented is needed if you need transferred data arrived in exactly same order. It might be bad if you send a file and your data on 128th byte is swapped with 129th because of delay. The connection-oriented ensure packet to arrive in exact order with exact size.
A connectionless is needed if you need transferred real time data regardless of order. You might consider streaming is one of example. One or two packet arrive late (arrive out of order or maybe not arrive at all) is not a problem while you get real-time update of video (a football match maybe).
Unix and it’s successor have sockets. Windows too, in it’s own way, has socket. Every modern OS must have socket as a standard way to communicate.
Next I will show you how to socket-programming in general way.