Audio Processing #1: Basic Terminologies

Networking#1 Socket Programming

In this post, almost all the terms related to socket programming is going to be discussed. Once a strong foundation on these concepts is built, thereafter an example on how to write socket program to establish networking between server and client will be discussed. Socket and twisted library from python will be used here and the prerequisites for this post are basics of networking and some knowledge of writing code in python.

In the inception, some of the networking vocabularies are discussed.

port number: It's a 16-bit number: ranging from 0-65535. Let us make a clear understanding about what a port is?. A port is nothing but the physical or virtual connection point. Physical connection point where physical wires, USB devices, pen-drive, routers, modems, and other peripheral devices can be connected. An example of Physical connection port is shown in Fig-1. While, virtual connection ports is the part of TCP/IP networking, these ports allow software applications to share hardware resources without physically interfacing with each other. Computer and Router automatically manage the network traffic travelling via their virtual ports. Look at Fig-2 for making a better understanding of virtual connection port.

Fig-1: Physical connection ports

Fig-2: Virtual Connection port

services: a software that performs automated tasks, responds to the hardware events, and listen to data requests from the other software. In an OS these services are loaded at the system startup automatically and keep on running without user interaction. As an instance, there is a services that responds to the keyboard shortcut each time the shortcut button in pressed. Fig-3 shows services running on windows OS.

Fig-3: Windows services running in background.

server: A computer that shares resources to the client machines over the network is known as server. Client sends request to the server and server is there to respond accordingly. Server and Client communication can be better understood from the Fig-4.

Fig-4: Server and Client network


TCP: Transmission control protocol is abbreviated as TCP. Its a connection oriented protocol that means it establishes the connection prior to the communication between two any two computers. Just to illustrate this concept, an example of client and server is taken in Fig-5. To know more about such protocols click here.

Fig-5: Example of TCP protocol.

 

Python: Only basic knowledge of python is enough to follow the further tutorial.

socket example: Remember that the server is going to accept requests from the clients and sends response to the clients. Server and client codes are given below. Both the codes are tested on the same computer and hence the 'localhost' is mentioned in the client code instead of the IP address of the server.

SERVER CODE

import socket
# creating server socket
s = socket.socket()
print('socket created')
# binding this socket to local host and port
s.bind(('localhost', 9999))
# start listening to the clients max client=3
s.listen(3)
print('waiting for connection')
while True:
    # accepting the connection from client
    c, address = s.accept()
    # reading the name from client and decoding it as the data between server
    # client is transfered in the form of bytes
    name = c.recv(1024).decode()
    # printing the ip and name of the client on the server console
    print('connected with: ', address, name)
    # sending the response to the client
    c.send(bytes('welcome to Asif server', 'utf-8'))
    # closing the connection
    c.close()

Here in the server code, firstly the socket module from python library in imported, thereafter a socket object is created with default values as IPv4 and TCP connection. Server is going to run on the localhost with port number 9999(most likely to be available: user can choose it any number in the desired range) and hence the server object is bound to the localhost and port number using bind() function. Secondly, its time to listen to the connection requests from client, here argument 3 means: if a server is processing a request it can only put 3 connection requests in waiting state and 4th connection request will be refused.

Finally, while loop will be executed every time a connection request from the client is made. Server accepts the client request and takes client name and print it on server console, and in response server sends "Welcome to Asif's server" message to the client and closes the connection with the current client.

CLIENT CODE

import socket
# creating socket for client
c = socket.socket()
# connecting the client to server
c.connect(('localhost', 9999))
# taking input from client as client name
name = input('Enter the name: ')
# sending name of the client to the server
c.send(bytes(name, 'utf-8'))
# printing the response received from the server
print(c.recv(1024).decode())

This is example of client code when client code and server code are running on the same machine. This is reason why localhost is written in the connect() function. If you want to connect to the server from a different computer then you must write the server IP address in place of localhost.


twisted library

Comments