12. Socket Programming Based Group Chat Application - Part 1



Hello folks!
Welcome to this new section of blog series. In this section we will see theoretical background as well as code related to project based on socket programming,o in this perticular blog
Sockets (aka socket programming) is a program that enables two sockets to send and receive data, bi-directionally, at any given moment.It works by connecting two sockets (or nodes) together and allowing them to communicate in real time, and is a great option for building a myriad of apps.
Internet-connected applications that need to operate in realtime greatly benefit from the implementation of sockets in their networking code. Some examples of apps that use socket programming are:
·       Web pages that show live notifications (Facebook, Twitch, eBay)
·       Multiplayer online games (League of Legends, WoW, Counter Strike)
·       Chat apps (WhatsApp, WeChat, Slack)
·       Realtime data dashboards (Robinhood, Coinbase)
·       IoT devices (Nest, August Locks)
Socket:
Sockets are basically used for inter process communication within single or over the network systems. So sockets are basically two endpoints of the communication via which we can communicate. So socket represents a single connection between exactly two pieces of software but we can also communicate between multiple piece of softwares but this requires multiple sockets.Sockets are also used for unidirectional as well as bidirectional communication
Now as we know if we have to work on the network then we have to deal with IP addresses also because each and every system on the network is recognized by its IP address.In this we have one server and many clients to take the service of the server. Notice that the client needs to know the existence of and the address of the server, but the server does not need to know the address of (or even the existence of) the client prior to the connection being established. Notice also that once a connection is established, both sides can send and receive information. 
Socket Types:
There are two widely used socket types, stream sockets, and datagram sockets. Stream sockets treat communications as a continuous stream of characters, while datagram sockets have to read entire messages at once. Each uses its own communications protocol. Stream sockets use TCP (Transmission Control Protocol), which is a reliable, stream oriented protocol, and datagram sockets use UDP (Unix Datagram Protocol), which is unreliable and message oriented.
The address of a socket in the Internet domain consists of the Internet address of the host machine (a unique 32 bit address, often referred to as its IP address). In addition, each socket needs a port number on that host. Port numbers are 16 bit unsigned integers. The lower numbers are reserved in Unix for standard services. For example, the port number for the FTP server is 21. It is important that standard services be at the same port on all computers so that clients will know their addresses. However, port numbers above 2000 are generally available
API to establish socket
In server side:
Socket system calls are declaired in the linux system at standard path in the <sys/socket.h> header file.
Firstly we have to make socket with socket() call. When a socket is created, the program has to specify the address domain and the socket type. Two processes can communicate with each other only if their sockets are of the same type and in the same domain. There are two widely used address domains, the unix domain, in which two processes which share a common file system communicate, and the Internet domain, in which two processes running on any two hosts on the Internet communicate. Each of these has its own address format
Bind the socket to an address using the bind() system call. For a server socket on the Internet, an address consists of a port number on the host machine.
Listen for connections with the listen() system call
Accept a connection with the accept() system call. This call typically blocks until a client connects with the server.
In client side:
In the client side also we have to make socket,Connect the socket to the address of the server using the connect() system call
Send and receive data. There are a number of ways to do this, but the simplest is to use the read() and write() system calls.
In next blog we will take some more information about socket ,socket programming and TCP/IP

 Reference :
[2] www.emlogic.com ,dt 3th april, 2020

By :
Ashutosh Bardapurkar    (k-05)
Hrishikesh Deshpande    (k-16)
Archit Hiwrekar        (k-23)
Chinmay Kapkar    (k-33)

14 comments: