14. Course Project Details



Hello folks!
Welcome to this last blog of our blog series. In the previous blog we have studied basics of socket programming,Now in this blog we will see the code of project, socket programming based group chat application

Code of the project

1) SERVER code :
import socket
import threading

print("Server Code\n")
class Server:
clients = []
names = []

    def create(self):
        self.s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        self.port = 25000
        self.name = input("Enter name : ")
        self.s.bind(('', self.port))
        print("Socket binded to port: " + str(self.port))
        self.chatWindow()
        return

    def sender(self):
        while True:
            msg = input()
            for i in self.clients:
                self.s.sendto(bytes(self.name + " : " + msg, "utf-8"), i)

    def receiver(self):
        while True:
            msg, self.addr = self.s.recvfrom(1024)
            if(self.addr not in self.clients):
                print("Message from " + str(self.addr) + " : " + str(msg.decode("utf-8"))+"\n")
                self.clients.append(self.addr)
                self.names.append(str(msg.decode("utf-8")))
                self.s.sendto(bytes(self.name, "utf-8"), self.addr)
                print("Name sent to "+str(self.addr)+"\n")
            else:
                print(self.names[self.clients.index(self.addr)] +
                      ": " + str(msg.decode("utf-8"))+"\n")
                msg = str(msg.decode("utf-8"))
                for i in self.clients:
                    if(i != self.addr):
                        self.s.sendto(
                            bytes(self.names[self.clients.index(self.addr)]+" : " + msg, "utf-8"), i)

    def chatWindow(self):
        print("Chat Messenger Server\n")
        threadS = threading.Thread(target=self.sender)
        threadR = threading.Thread(target=self.receiver)
        threadS.start()
        threadR.start()

if __name__ == '__main__':
    server = Server()
    server.create()

2) CLIENT code :

import socket
import threading

print("Client Code\n")
class Client:
clients = []
names = []

    def create(self):
        self.name = input("Enter name : ")
        self.ip = socket.gethostbyname(socket.gethostname())
        self.port = 25000
        self.c = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        print("Socket binded successfully")
        self.c.sendto(bytes(self.name, "utf-8"), (self.ip, self.port))
        print("Chat Messenger Client\n")
        self.chatWindow()

    def chatWindow(self):
        threadS = threading.Thread(target=self.sender)
        threadR = threading.Thread(target=self.receiver)
        threadS.start()
        threadR.start()

    def sender(self):
        while True:
            msg = bytes(input(), "utf-8")
            self.c.sendto(msg, (self.ip, self.port))

    def receiver(self):
        while True:
            msg, addr = self.c.recvfrom(1024)
            if(addr not in self.clients):
                self.clients.append(addr)
                self.names.append(str(msg.decode("utf-8")))
                print("Message from " + str(addr) + ": Name : " + str(msg.decode("utf-8"))+"\n")
            else:
                print(str(msg.decode("utf-8"))+"\n")

if __name__ == '__main__':
    client = Client()
    client.create()
    client.chatWindow()

The details of this code are explained in links given below


Details of the course project :

Course Project Title:Socket programming based group chat application
Description of project :
Course Project Report Link : Report link
Course Project Presentation Link : Presentation Link


Topic 1:
Presenter's Info:
Student Name : Archit Hiwrekar
Div : K
Roll No : 23
GRNO : gr no 1710994
Topic 1 Video Link : Video link

Topic 2:
Presenter's Info : 
Student Name : Chinmay Kapkar
Div : K
Roll No : 33
GRNO : 1710732
Topic 2 Video Link : Video link

Topic 3:
Presenter's Info : 
Student Name : Hrishikesh Deshpande
Div : K
Roll No : 16
GRNO : 1711063
Topic 3 Video Link :  Video link

Topic 4:
Presenter's Info:
Student Name : Ashutosh Bardapurkar
Div : K
Roll No :5
GRNO :1710459
Topic 4 Video Link : Video link

13. Socket Programming Based Group Chat Application - Part 2



Hello folks!
Welcome to this blog series.In previous blog we have seen basic theoretical background related to project based on socket programming ,now in this blog we will see some more concepts such as socket,TCP/IP which are related to this project.
In this project we are trying to establish connection between multiple people at the same time so they are able to chat with person by establishing server and client side separately using TCP IP protocol TCP/IP, or the Transmission Control Protocol/Internet Protocol, is a suite of communication protocols used to interconnect network devices on the internet. TCP/IP can also be used as a communications protocol in a private computer network (an intranet or an extranet). TCP/IP can be used to provide remote login over the network, for interactive file transfer, to deliver email, to deliver webpages over the network and to remotely access a server host's file system.
TCP/IP Layer :

                                               Figure reference : www.guru99.com

socket programming interface provides the routines required for interprocess communication between applications, either on the local system or spread in a distributed, TCP/IP based network environment. Once a peer-to-peer connection is established, a socket descriptor is used to uniquely identify the connection.
The sockets are the endpoints of any communication channel. These are used to connect the server and client. Sockets are Bi-Directional. In this project, we setup sockets for each end and setup the chatroom system among different clients through the server. The server side has some ports to connect with client sockets. When a client tries to connect with the same port, then the connection will be established for the chat room. Every time a user connects to the server, a separate thread is created for that user and communication from server to client takes place along individual threads based on socket objects created for the sake of identity of each client.
There are basically two parts. The server side and the client side. When the server side script is running, it waits for any active connection request. When one connection is established, it can communicate with it. In this case we are using localhost. If machines are connected via LAN or same wifi hotspot, then we can use IP addresses to communicate. The server will display its IP. From the client side the IP address of the server to connect. A message sent to from server side act as broadcast message to all the clients.
To use a socket object in the program, we first start off by importing the socket library.The type parameter is set to Socket Stream, also the default which enables “sequenced, reliable, two-way, connection-based byte streams” over TCP protocol. This code makes a socket object, and binds it to localhost’s port 8080 as a socket server. When clients connect to this address with a socket connection, the server listens for data, and stores it in the “data” variable. When a client connects, the server calls accept() to accept, or complete, the connection. The client calls connect() to establish a connection to the server and initiate the three-way handshake. 
The handshake step is important since it ensures that each side of the connection is reachable in the network, in other words that the client can reach the server and vice-versa. It may be that only one host, client or server, can reach the other.
In the middle is the round-trip section, where data is exchanged between the client and server using calls to send() and recv().
At the bottom, the client and server close() their respective sockets.

Figure & theory reference

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

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)