Report Chat Application
Report Chat Application
Abstract:
Theory:
This project is designed with python, tkinter library and TCP
sockets.
What is Python?
What is tkinter?
Tkinter is the standard GUI library for Python. Python when combined with
Tkinter provides a fast and easy way to create GUI applications. Tkinter
provides a powerful object-oriented interface to the Tk GUI toolkit.Creating a
GUI application using Tkinter is an easy task. All you need to do is perform the
following steps −
1. Import the Tkinter module.
2. Create the GUI application main window.
3. Add one or more of the above-mentioned widgets to the GUI application.
4. Enter the main event loop to take action against each event triggered by
the user.
Tkinter Widgets:
Tkinter provides various controls, such as buttons, labels and text boxes used in
a GUI application. These controls are commonly called widgets.
Tk(screenName=None, baseName=None, className=’Tk’, useTk=1): To
create a main window, tkinter offers a method ‘Tk(screenName=None,
baseName=None, className=’Tk’, useTk=1)’. To change the name of the
window, you can change the className to the desired one. The basic code
used to create the main window of the application is: m=tkinter.Tk() where m
is the name of the main window object
m.mainloop()
import tkinter
m = tkinter.Tk()
'''
widgets are added here
'''
m.mainloop()
There are a number of widgets which you can put in your tkinter application.
Some of the major widgets are explained below:
activebackground: to set the background color when button is under the cursor.
activeforeground: to set the foreground color when button is under the cursor.
bg: to set he normal background color.
import tkinter as tk
r = tk.Tk()
r.title('Counting Seconds') button = tk.Button(r, text='Stop',
width=25, command=r.destroy) button.pack() r.mainloop()
Output:
2. Entry : It is used to input the single line text entry from the user.. For
multiline text input, Text widget is used.
The general syntax is: w=Entry(master, option=value)
master is the parameter used to represent the parent
window.
There are number of options which are used to change the format of the widget.
Number of options can be passed as parameters separated by commas. Some of
them are listed below.
bd: to set the border width in pixels. bg: to set the normal
background color. cursor: to set the cursor used. command:
to call a function. highlightcolor: to set the color shown in
the focus highlight. width: to set the width of the button.
height: to set the height of the button.
What is socket?
Definition:
The client also needs to identify itself to the server so it binds to local port
number that it will use during connection. This is usually assigned by system. If
everything goes well, the server accepts the connection. Upon acceptance, the
server gets a new socket bound to the same local port and also has its remote
and endpoint set to the address and port of client.
It needs a new socket for connection requests while trending to the needs of
connected client.
On the client side, if the connection is accepted a socket is successfully created
and the client can use the socket to communicate with the server.
The client and server can now communicate by writing to or reading from their
sockets.
To create a socket, you must use the socket.socket() function available in socket
module, which has the general syntax −
s = socket.socket (socket_family, socket_type, protocol=0)
Here is the description of the parameters −
• socket_family − This is either AF_UNIX or AF_INET, as explained
earlier.
• socket_type − This is either SOCK_STREAM or SOCK_DGRAM.
• protocol − This is usually left out, defaulting to 0.
Once you have socket object, then you can use required functions to create your
client or server program.
The chat server does the following things
1. Accept multiple incoming connections for client.
2. Read incoming messages from each client and broadcast them to all other
connected clients.
We will be using TCP sockets for this purpose, and therefore we use AF_INET
and SOCK_STREAM flags. We use them over UDP sockets because they’re
more telephonic, where the recipient has to approve the incoming connection
before communication begins, and UDP sockets are more post-mail sort of thing
(anyone can send a mail to any recipient whose address s/he knows), so they
don’t really require an establishment of connection before communication can
happen. Clearly, TCP suit more to our purpose than UDP sockets, therefore we
use them.
def accept_incoming_connections():
This is just a loop that waits forever for incoming connections and as
soon as it gets one, it logs the connection (prints some of the
connection details) and sends the connected client a welcome
message. Then it stores the client’s address in the addresses dictionary
and later starts the handling thread for that client. Of course, we
haven’t yet defined the target function handle_client().
def handle_client(client):
Naturally, after we send the new client the welcoming message, it will reply
with the name s/he wants to use for further communication. In the
handle_client() function, the first task we do is we save this name, and then send
another message to the client, regarding further instructions. After this comes
the main loop for communication: here we recieve further messages from the
client and if a message doesn’t contain instructions to quit, we simply broadcast
the messsage to other connected clients (we’ll be defining the broadcast method
in a moment). If we do encounter a message with exit instructions (i.e., the
client sends a {quit}), we echo back the same message to the client (it triggers
close action on the client side) and then we close the connection socket for it.
We then do some cleanup by deleting the entry for the client, and finally give a
shoutout to other connected people that this particular person has left the
conversation.
def broadcast(msg, prefix="")
This is pretty much self-explanatory; it simply sends the msg to all the
connected clients, and prepends an optional prefix if necessary. We do
pass a prefix to broadcast() in our handle_client() function, and we do it so
that people can see exactly who is the sender of a particular
message. That was all the required functionalities for our
server.
1
s.bind()
2
s.listen()
3
s.accept()
THE CLIENT:
This module manage any client willing to connect on a specific host and port.
It authorizes the client to send and receive messages.
To do:
• Write or improve docstrings
receive():
Why an infinite loop again?
Because we’ll be receiving messages quite non-deterministically, and
independent of how and when we send the messages. We don’t want this to be
a walkie-talkie chat app which can only either send or receive at a time; we
want to receive messages when we can, and send them when we want. The
1
s.connect()
ADVANTAGES:
• If developed further by adding a database, frontend and buying a server
place on the internet, this project can be converted into a real time chat
application.
LIMITATION:
• The clients can only chat by connecting to a local server.
• Two clients on different networks cannot chat together.
Working model:
1.SERVER IS RUNNING AND WAITING FOR CLIENT
CONCLUSION:
In this project we have used the basics of networking in python. We
also learned how to make a GUI for our application. This project
teaches us how to create a basic chat application by creating a local
network and using TCP sockets.