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)

11. Cafeteria Management System Code - Part 2


Hello folks!
Welcome to the blog series.In the previous blog we have seen about the libraries that we used in our program as well as the declaration of class, methods and variables. So in this blog we will continue with the remaining part code which includes global variables as well as main() function.

So as you can see we have used the standard namespace for using standard input and output. Next is the flag which is a global variable which is used so that the first part of the program which gives the user output whether the program has started runs only once. Another variable is of type ofstream which is used for writing every bill printed into a log file.

Another global variable is a pointer which stores the address of objects of class Cafe. In the main function we have declared two variable cmd which stores the option number for the item which the customer has ordered and table_number which stores table number. As explained earlier the use of flag variable the user gets to know the program has started. Also we are opening the Cafe_log.txt file to write the new bills. Next we are taking the input for table number and corresponding option for the item that the customer has ordered.
According to the option that the user has entered the program asks for the quantity for that item that the customer wants to order or print the bill or exit from the program. If the user opts for the option for the bill we also simultaneously write the bill the log file and if the user wants to exit the program then we close the log file which was opened earlier.

If the user enters in inappropriate command then an message is displayed for the same. So this is all about the code.The link of the code is attached below. In the next blog we will discuss about theoretical background of the project which is based on socket programming.


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

10. Cafeteria Management System Code - Part 1


Hello folks!
Welcome back to this blog series. In the previous blog we have studied advance ways of file handling .
Now In this blog we will see the code for Cafeteria Management System which is an application of object oriented programming studied in prior blogs. So lets start by knowing the libraries used.

Theee libraries that we required in order the implement the code are mentioned above. I will explain you some of the important libraries we needed. So the first one is the iostream library which contains the input output streams which are needed to make the user experience more better.
Next is the fstream library which contains necessary functions for file handling. The stdlib.h library is  used for dynamic memory allocation so as to make the space complexity low. Another important library is string.h which is required to perform string operations. We have also used a macro ‘max_value’ which is used to store the maximum number of tables available in the cafe.
Next task is to create a class and then create objects of this class which denote food serving tables of cafe.

So as you can see we have declared a class named Cafe which has private members price which is an array containing prices of each item present in the menu card which we will be displaying in the main() method. Quantity variable contains quantity of each item that the customer has ordered and bill contains the total amount that the customer needs to pay. In the constructor of the class we are setting values of arrays price and quantity to zero assuming that no customer is there on the table and cafe is just started. Next task is to set which items needed to be displayed in the menu. For example, we have taken four items as shown in the above picture. Also we have set the bill value to zero.
Now we need to create a method which would actually do the processing of data obtained from customer which includes getting the order, changing the appropriate values of quantity array and to calculate the total bill.


As you can see in the above image we have created a function named option which takes item number of the item ordered as input and changes values of quantity array and variable bill.


Next function is display which takes a fstream pointer as input. In this function we are basically display the items that we ordered and their respective quantities along with price and total amount. Simultaneously, we are also storing this bill as text in a file using the file pointer that we took as input.
Finally, in the destructor we are assigning bill variable to zero assuming that the previous customer has left the cafe and new customer has arrived. Also, we are assigning zero value to all elements of price and quantity array respectively.
So this is the end of class declaration and in the next blog we’ll explore the main() function and try to understand how the user will interact with the program. We will also examine the need for some global variables that we have used in the program. 

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

9. Advance way of handling files in C++




Hello folks!
Welcome to this blog series. In the previous blog we have studied basic file handling in C++. In this blog we will see some more advance ways of file handling in C++, so let’s get started.

Advance way of handling files in C++

There can be different purpose of opening a file. For example to write on the file, to read from the file, etc. These are the different modes in which we can open a file in C++.


Mode
Description
ios::app
To open a text file for appending purpose.
ios::ate
To open a file for output purpose and move the read/write control to the end of the file.
ios::in
To open a text file for reading purpose.
ios::out
To open a text file for writing purpose.
ios::trunc
To truncate the file content before opening a file, if file already exists in our system.


















Let’s understand this with an example code.

Code:
Output:
Files before running the code:
Files after running the code: 

So we now know how to play with files in C++. So we will meet you in our next blog where we will discuss, how we implemented the system in C++ with its code explained.

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

8. Basics of File Handling in C++


Hello folks!
Welcome to this blog series. In the previous blog we have studied see how Dynamic memory is allocated in C++.
Now in this blog we will study how to handle files in C++.  So why do we need file handling knowledge while designing our system? We require File handling because know when our system is fully build and given to the user to operate it, the user of the system needs to know how much transaction had occurred since past check for various reasons such as to calculate the profit the Cafeteria is making. Number of items sold for the quantity of goods to be purchased etc. So let’s get started with this blog session.

Basics of File Handling in C++

The Basic file handing library in C++ is fstream. The fstream library contains
1.     ofstream object/data type  which Creates and writes to files,
2.     ifstream object/data type  which Reads from files
3.    fsrteam class which contains file handling functions.
Now if we want to create a file and write into it, we basically use ofstream or fstream object and specify the name of file to be created.
We use insertion operator (<<) to write in the file. Let’s take an example to see how it is done.

Code:
Output:
Text File:

Similarly if we want to just read the file, we use ifstream or fstream object and specify the name of file to be read.
We use extraction operator (>>) to read from the file. Let’s take an example to see how it is done.
We will read from the above created file for understanding purose.

Code:
Output:

Some of us will get a doubt that why did I close the file, because in C++ by default all the files opened are closed and all related data is released, the answer to this question is “as a good programming practice we should write file closing syntax is our code”. Not all the programming languages out there have such functionality.
So now we can do basic file handling in C++.
Some more advance ways of file handling in C++ will be discussed in our next blog.

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