What is a socket ?
Socket is defined as the endpoint of communication between two processes. These two entities can be two processes running in the same system or in systems connected by a network.
To understand better about sockets, let us see how a webserver and client communicates. How do you think they communicate internally ? It is actually not possible for the client to directly talk to the server running somewhere without the knowledge of the Operating system that runs in the location where server runs. So the Operating system is the one that should facilitate the information it got from client to the server process running in it. So having known that the OS should facilitate this, we should know how it does it .
The Operating system gives a slot which is exposed to the outer world, where the messages is allowed to be written. So the Client writes to this slot. As soon as a message is written to this slot. The OS needs to take this message and hand it over to the server process to which the slot is mapped to. So the message from the client comes via this slot and reaches the server, with the facilitation done by the OS. This slot is what is called as socket.
Can we confirm our understanding ?
To confirm that the socket is the intermediate point between server and client, let us do a test by writing directly to the socket instead of calling from client and see if it reaches the server. If it happens then we are right.
Run a simple python Flask server.
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
print "Done !"
return "Hello World!"
if __name__ == "__main__":
app.run()
Now write the following python script to write to the socket. The socket is identified by the hostname( where the server process runs) and the port number. So we write to this socket.
import socket
clientsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
clientsocket.connect(('localhost', 5000))
clientsocket.send('hello')
Now if you run the socket program and you will find that the following is printed to console of the flask server
127.0.0.1 - - [15/Aug/2016 00:12:43] code 400, message Bad request syntax ('hello')
127.0.0.1 - - [15/Aug/2016 00:12:43] "hello" 400 -
Just an indication that our server was called on writing to socket.
So here the socket was identified by the tuple
('localhost', 5000)
This socket aids communication between server and client in the network. This socket is hence called Network Socket. .
Have you taught of a scenario where two processes running in the same system needed to communicate ?
Yeah this too can take place via a socket. These type of socket is called domain socket. The process which needs to communicate to the other process, places it in the socket and the information is delivered to the other process.
Have you seen a use case for Domain sockets ?
This is how the writing to syslog by a process, takes place in unix. The process writes the information to this socket. The syslogd service which runs in the background and has the socket mapped to it and which is actually listening to this socket will be notified on information arrival. Then this syslogd will pick up that info and write it to the /var/log/syslog.
Can you confirm it ?
The following is the code.
import socket
clientsocket = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
# the socket to which the syslogd is listening to is /dev/log
clientsocket.connect('/dev/log')
clientsocket.send('*****************hello*****************')
Now when you run this code, you will see the ************hello************** entry in the /var/log/syslog.
This again proves our point about sockets.
Note : this is just for learning. Actually python provides an api to do the writing to syslog easier. This is the logging.handlers.SysLogHandler api.
How to identify sockets in unix ?
As in linux everything is just a file, how to differentiate between the files, directories and sockets On doing ll command, the first character is the special file designator character 's' here denotes it is a socket
for eg.
ll /run/systemd/journal/dev-log
srw-rw-rw- 1 root root 0 Aug 12 18:11 /run/systemd/journal/dev-log=
References: