How We Implement Chat Application Over UDP Protocol Using Python Programming

We will be making 2 simple and interactive applications in python, one of which will send data over UDP to a server, and another on the server which will be reading the data and processing it.

Given below is the code of the program and the explanation is done via comments, so you can simply copy, paste the code and run it.

This is the file on the server side,

————————————————————————————————————————————

#!/usr/bin/python2

import socket

”’

We import the socket module which will be used to create new socket through which data will be sent.

The socket module has a lot of other names defined in it which we can see using the dir(socket) function.

In the socket module, we use another module called socket which creates a new socket using the given address family, socket type and protocol number. Check it using dir(socket.socket)

socket.socket takes 3 arguments.

socket.socket([family[,type[,proto]]])

For the IPv4 address family, we use AF_INET.

Socket type ‘streaming’ means the data which requires a stable connection e.g. TCP.

Socket type ‘datagram’ means the data which does not require a stable connection and sends bursts of data without expecting replies e.g. UDP.

Here, for UDP, we use SOCK_DGRAM.

By default protocol which SOCK_DGRAM uses is UDP, hence we do not have to mention anything in the protocol argument.

”’

s=socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

# To bind the socket to an address, we use socket.bind(address)

# “” means it binds to all ip address of your system, and 5555 is the port you need to bind to, so the server can listen to it.

# you can check with “netstat -unlp” command after running this program

s.bind((“” , 5555))

”’

To keep on listening to the data and not let the program terminate, we write the following loop.

To receive the data, we use socket.recvfrom(bufsize[, flags]) which receives data from the socket.

The return value is a pair (string, address) where string is a string representing the data received and address is the address of the socket sending the data.

”’

while True:

            datareceive=s.recvfrom(100)

            clientip=datareceive[1][0]

            data=datareceive[0]

            print clientip , “  :  ” ,  data

—————————————————————————————————————————————————————–

# The program on the client is pretty much the same,

#!/usr/bin/python2

import socket

import sys

s=socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

”’

By default, any default client port is opened but we select one by specifying it like this.

”’

s.bind((“” , 3333))

# To send data to the socket, we use socket.sendto(data to be sent, address in tuple form)

serverip=sys.argv[1]

while True:

            data=raw_input(“Enter your data to send : “)

            s.sendto(data ,  (serverip , 5555))

# The codes are ready to paste, but it is recommended to write them yourself icon smile How We Implement Chat Application Over UDP Protocol Using Python Programming