Chat Application using Python
The objective of this project is to design and develop a desktop chat application that allows chatting between two users through a server. The server also handles interactions between many other users. To make this process work we used the Python Network Programming concepts. Python has built in modules which makes it possible to access the basic socket support in the underlying operating system, allowing us to implement the clients and servers for both connection-oriented and connectionless protocols. The basis for this chat application is the fundamental concept in Networking – Socket Programming. Sockets: Sockets are the endpoints of a bidirectional communications channel. Sockets may communicate within a process, between processes on the same machine, or between processes on different continents. Sockets may be implemented over a number of different channel types like UDP, TCP etc. For our project we used the UDP (User Datagram Protocol), a connectionless protocol which provides faster interactions between a client and a server.
To create a socket, Python provides the socket.socket() function which is available in the socket module with the syntax: s = socket.socket(socket_family, socket_type, protocol=0) socket_family : This is either AF_UNIX or AF_INET socket_type: This is either SOCK_STREAM or SOCK_DGRAM protocol: This is usually left out, defaulting to 0
Socket programming: Programming using the sockets for the client and server interaction forms the basis of our chat application Chat Application: Client-Client interaction through a common server: We developed two modules, one for the server and one for the client. The client program is built with GUI features and serves as the chat interface for the users. To start chatting, the clients connect to the server and then one of the clients connects to the other through the interface. The messages from a client reaches the server and the server forwards the same to the other client based on the information in the incoming message and address.
Features of the Python program we used: • Python is a weakly typed language and it has no data types. Hence we assign variables and constant with just its name • We used the objects of the input libraries of Python to access their functions • We used Dictionary from Python to store the client name and address. Dictionaries are associative arrays or hash tables that consists of pairs(called items) of keys and their corresponding values The syntax is dict = {‘A’: 1, ‘B’:2, ‘C’:3}
Here A, B, C are keys and are unique. 1,2,3 are values which are accessed through the syntax :
dict[‘A’] //returns 1
• We defined our own functions to perform different tasks including the GUI part. Python allows creation of user defined functions using the def keyword • We used the basic control statements like the for and while loops to read a dictionary, to read the incoming messages etc., • We used the try and except commands to handle exceptions • We used the built in GUI module called Tkinter • Multithreading: A chat application requires that a client must be able to send and receive messages at the same time. To achieve this we used the concepts of multithreading in Python. We used the threading module in the client program to receive the messages sent from the server. This threading method starts the receive function and keeps running in parallel to the function that sends the messages.
Compiler: • Download the Python interpreter 2.7.8 from https://www.python.org/downloads/ • Install in the folder of your preference • This interpreter comes with a Python IDE called IDLE. The .py files can be opened and executed using IDLE. • To find IDLE just search from Windows home screen • F5 in IDLE runs the program Running the project: • First open and run the finalServer.py file to start the server • Now open and run the finalClient.py file to get the GUI • Give a ‘username’ of 6 characters and also a name of 6 characters in the ‘connect to’ field. • Now click the login button to connect the above client to the server • Now open one more finalClient window (Client 2) and execute the file to get another GUI • In the above GUI, the username field must be the same as the name in ‘connect to’ field for the Client 1, leave the ‘connect to’ field in Client2 window as empty • Now click the login button to connect Client 2 to the server • Now the two clients, Client1 and Client2 are connected and chatting can begin