Definition of socket programming
Socket programming refers to the way of establishing a connection between two different end points (nodes). These two end points are actually one of the 65,535 ports found in a computer. These ports can be found in a single computer, or could belong to two different computers. Each port specifies a specific service (most of them are unused). So in simpler words, socket programming is all about connecting 2 services, inside, or between devices.
So the term socket, in this context is nothing but a port.
Having some basic knowledge about networking is really important to understand this topic. We are also going to use python to make a socket connection practically.
What is a port ?
- HTTP (Hypertext Transfer Protocol) – It is a service that helps to transfer hypermedia data. Such as html pages, audio, videos etc through the internet. It has port number 80
- HTTPS (Hypertext Transfer Protocol Secure) – It is just a secure version of HTTP service. Sending data through HTTPS is more secure as is uses some sort of cryptography that makes it difficult for an intruder to read the sent messages. It has port number 443 assigned to it
- SSH (Secured Shell) – It is a service used to control one device / software, remotely from other device / software. It has port number 22 assigned to it.
Of course there many ports out there, and have different functions. But you should know, the ports we just talked about right now are the reserved ones. Which means they have specific characteristics which cannot be changed. There are other like these too. Here are some of the others. And out of 65,535 ports, most are not specifically assigned for a task, and can be used for other things by the user, which is what socket programming uses the most
Client and server
As you might know, connecting two different devices, actually refers to transfer of data between them. The device which requests the other for the connection to be made is called the client. And the other one is called a server. What actually happens is that, one of the port of the client is connecting to one port of the server. Hence these ports are called client and server respectively.
As you might also know, that there are internal links between services in a computer too. So ports connecting them could also make a client / server pair.
Examples of socket connections
Well most of the connections you see are in fact a kind of socket connections. For example when you are connecting to youtube (lets say the ip your DNS found is 184.108.40.206), you might know what you are actually typing is https://youtube.com or https://220.127.116.11 in the search engine.
https is service which runs on the port number 443. So basically you are connecting to the port 443 of the IP address 18.104.22.168, the port you are using to connect is a randomly chosen unused ports (Lets take it 9991 here). Hence to make a connection to a device, you first need to find its IP. This is how the device is found by your system. And to actually make a connection, you need to specify the service you are trying to reach, which is the port number.
You call the combination of your ip and port, a socket. And the combination of ip and port of the server, as a socket too.
Socket programming using python
Designing a server
import socket server_IP = '192.168.1.39' port = 9999 server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server.bind((server_IP, port)) server.listen(4) connection = True while connection: comm_socc, addr = server.accept() message = comm_socc.recv(1024).decode('utf-8') print(addr, '-----', message) comm_socc.send('OKay got it'.encode('utf-8')) if message == 'close': connection = False print('disconnecting from', addr) comm_socc.close() server.close()
Designing a client
import socket target = '192.168.1.39' port = 9999 client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) client.connect((target, port)) client.send('close'.encode('utf-8')) print(client.recv(1024).decode('utf-8'))
Alright lets see what we did here. First we imported a pre installed python library called socket. We create a socket for both client and server. Both the sockets will be communicating with ip address and will be making a tcp connection. Hence the arguments socket.AF_INET and socket.SOCK_STREAM.
To make a server we use the bind function. Passing it our IP and port. Notice how the port we are passing is should be one of the unused ones. I chose 9999. Then we start the server by starting listening and teling it max number of devices which can be connected to it, which is 4.
For the client, instead of binding, we simple connect it to the server using its ip and port. And send a message. Remember that a message must be encoded before sending, and decoded before printing.
The server gets the message, prints it, sends a message to the client after that (this is possible because we are using a TCP connection, it would not have been possible with a UDP). The client receives the message and prints it.
So after running the server, and then the client, the client will get
OKay got it
and the server will get
('192.168.1.39', 20800) ----- hey bruh
And if the message was ‘close’ instead of anything else, the clients output would have been the same , but the server would get
('192.168.1.39', 20811) ----- close disconnecting from ('192.168.1.39', 20811)
And the server socket would have closed.
Note, the server.accept() gave us the socket and the address of the client (ip + port) of the client. And to close this specific connection, the server needed to close the client socket (comm_socc).
Also notice how the client has the same ip as the server. It is because I used the same computer to make both programs.
The above demo was just a very simple kind of socket programming you can do using python. Of course, there are many ways and techniques of making socket connections. We just scratched the surface. The application of socket programming is vast, Which I will be trying to show you in some of my future articles.