Creating Chat Servers using Socket Programming in Python

Creating Chat Servers using Socket Programming in Python

ARTH - Task 17 ??????

?? Create your own Chat Servers, and establish a network to transfer data using Socket Programing by creating both Server and Client machine as Sender and Receiver both. Do this program using UDP data transfer protocol.

?? Use multi-threading concept to get and receive data parallelly from both the Server Sides. Observe the challenges that you face to achieve this using UDP.?


What is Socket Programming in Python ?

Socket programming is a way of connecting two nodes on a network to communicate with each other. One socket(node) listens on a particular port at an IP, while the other socket reaches out to the other to form a connection. The server forms the listener socket while the client reaches out to the server.

What is UDP Data Transfer Protocol ?

User Datagram Protocol (UDP) is a communications protocol that facilitates the exchange of messages between computing devices in a network.

UDP divides messages into packets, called datagrams, which can then be forwarded by the devices in the network – switches, routers, security gateways – to the destination application/server. While UDP does not number or reassemble the datagrams, it does include port numbers in the datagram header that help distinguish different user requests and an optional checksum capability that can help verify the integrity of the data transferred.

What is Multi-threading in Python ?

Generally it is tendency of a program to execute it's statements one by one , so a statement is executed only when the statement or command prior to it has completed execution. Multi-threading is the ability of CPU to provide multi-threads to execute the program concurrently. Means if two commands are present in the program then they will be executed at the same time independently.

Let's begin with the Practical part -

To use socket and threading module one must install them. One can easily do that with the help pip3 python package installation command -

for socket module
# pip3 install sockets

for threading module
# pip3 install thread6        

In this task I have used two nodes one is Windows 10 node and other is RedHat Linux node running in the same windows system as a virtual machine.

The code for both the nodes is same -

No alt text provided for this image

Explanation of the Code -

  • First I have imported the required modules - socket and threading .
  • Then created a socket using socket.socket() function and the bound the socket to my IP of windows system and a random port number (2525).
  • Next we the declaration of function send() with takes input from user and send it to the Linux system continuously.
  • Next is the receive() function to receive data from the Linux VM.
  • Then I have created two threads using threading.Thread() function and assigned them the two functions send and receive so that the both function can run parallelly.
  • At last started the threads using start() function.

The Linux system also has the same code except for the host and sending IPs.

No alt text provided for this image

To start the chatting run both file at same time using Python3 command and start chatting -

# python3 <file_name>

No alt text provided for this image
No alt text provided for this image

The left command prompts is of windows localhost and the right one is of Linux VM.

Windows Prompt

No alt text provided for this image

Linux Prompt

No alt text provided for this image


Some Challenges faced while doing the Task --

  • First mistake I did was to name my python file "socket.py" because of this the python program was importing this code file instead of the original socket module and the program not recognizing the functions.
  • The next was the windows and Linux firewall was not allowing the sockets to receive messages. so I had to create rules for them.
  • The data in string data type can't be send over socket network for that we have to convert the string to bytes data type using encode() and decode() function.
  • Some of the messages were not reaching to the destination . This was because I had used UDP protocol which is a connectionless protocol i.e. while using UDP if any data packet is lost it is not retransmitted . UDP is faster than TCP because it doesn't wait for confirmation from receiver that data reached the destination or not. So you may some of the messages in windows command prompt were sent but not received on the Linux command Prompt.

THANK YOU FOR READING!!!!!!!!!!!!

要查看或添加评论,请登录

Vivek Sharma的更多文章

社区洞察

其他会员也浏览了