Socket Programming – Part 1

Network Socket
A network Socket is an end point of an inter-process communication flow across a computer network.
Socket is a single connection between two software applications.
Sockets are used for web browser, online gaming, FTP, SSH, P2P etc.

How to create a socket?

Creating a socket is similar to telephone call. Lets see how we make a telephone call –
1. First we buy 2 telephones one for the caller and one for the person whom we want to call.
2. Telephone should have unique telephone number so we assign telephone number to it.
3. On the receiver side we turn the ringer on to hear call.
4. Caller dial the telephone number.
5. Receiver answer the call.
6. Now caller and receiver can talk.
7. After some time they terminate the telephone connection.

Now lets see how to establish a socket –
1. A connection end point is created on both side.
2. Unique address is assigned to each end point.
3. One end point request for connection and other end point wait for connection call.
4. Connection established, data exchanged.
5. Data exchange end and connection terminated.

Various functions used for data exchange
1. socket( ) -> similar to buy a telephone
2. bind( ) -> similar to assign a telephone number to a telephone
3. listen( ) -> turn on ringer to hear call
4. connect( ) -> dial a telephone number
5. accept( ) -> waiting telephone answer call
6. send( ) / recv( ) -> talk/conversation
7. close( ) -> terminate connection

Lets see some basics before going into real socket programming.

1. MAC Address
– MAC stands for “ Media Access Control
– Unique identifier assigned to network adapter or network interface cards assigned by manufactures.
– Format – x:x:x:x:x:x
x is a hexadecimal value sperated by :
x lies between 00 to ff
eg – 00:1f:f4:5c:43:fe

2. OSI Model
OSI stands for “ Open System Interconnection Model
Used for universal system communication
Total 7 layers
Layer 1 – Application
Layer 2 – Presentation
Layer 3 – Session
Layer 4 – Transport -> use TCP/UDP
Layer 5 – Network -> use IPv4/IPv6
Layer 6 – Data Link
Layer 7 – Physical

3. Internet Protocols
i. IPv4 – Internet protocol version 4
– IPv4 size is 32 bits
Format – x:x:x:x
x is 8 bits
x lies between 0 to 255
eg – 192.168.85.2
Total number of ip addresses possible with IPv4 2^32= 4,294,967,296 ip addresses.

ii. IPv6 – Internet protocol version 6
– IPv6 size is 128 bits
Format – x:x:x:x:x:x:x:x
x is 16 bits
x lies between 0000 to ffff
eg – 3bfa:0:1254:0:400:f8ff:fa21:63d2
In above given ip address zeros can be ignored and can be written as 3bfa::1254::400:f8ff:fa21:63d2
Total number of ip addresses possible with IPv6 2^128 = 340,282,366,920,938,463,463,374,607,431,768,211,456 ip addresses.

4. Endianness
It descirbe the order in which the squence of bytes store in computer memory
Two types –
i. Big Endian – Store the most significant byte first (left to right)
Big-Endian
ii. Little Endian – Store the least significant byte fist (right to left)
Little-Endian

Leave a Reply

Your email address will not be published. Required fields are marked *