Python implementation of UDP
User Datagram Protocol is one of the oldest protocol in existence that has been used extensively in client/server applications. Based out of the principal communications protocol, called Internet Protocol (IP), UDP is an alternative for TCP which stands for Transmission Control Protocol.
When the client is started it send the server current time information and the server replies an acknowledgement saying how many bytes was received by the server. Since this program is run on the same machine and the messages are not sent via network there wont be any possible packet loss. We need to consider those aspects while writing code for real world applications.
Queries regarding which among the two protocol can be a better choice should be determined only after answering questions based on the context for which you are writing the code for. Since UDP is known for its unreliability, it is important that you use it right.
Without any error checking or error correction involved in data transmission UDP is often used in video chat applications because a fall in a packet or two doesn't affect final outcome.
The following post will be focused on developing a client server application that will implement using Python.
The program can be passed arguments namely - "client" and "server" and the program acts accordingly.
The program can be passed arguments namely - "client" and "server" and the program acts accordingly.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import argparse, socket | |
from datetime import datetime | |
MAX_BYTES = 65535 | |
def server(port): | |
sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM) | |
sock.bind(('127.0.0.1',port)) | |
print('Listening at {}'.format(sock.getsockname())) | |
while True: | |
data, address = sock.recvfrom(MAX_BYTES) | |
text = data.decode('ascii') | |
print('The client at {} says {!r}'.format(address,text)) | |
text = 'Your data was {} bytes long.'.format(len(data)) | |
data = text.encode('ascii') | |
sock.sendto(data,address) | |
def client(port): | |
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | |
text = 'The time is {}'.format(datetime.now()) | |
data = text.encode('ascii') | |
sock.sendto(data,('127.0.0.1',port)) | |
print('The OS assigned me the address {}'.format(sock.getsockname())) | |
data, address = sock.recvfrom(MAX_BYTES) | |
text = data.decode('ascii') | |
print('The server {} replied {!r}'.format(address,text)) | |
if __name__ == '__main__': | |
choices = {'client':client,'server':server} | |
parser = argparse.ArgumentParser(description='Send and receive UDP locally') | |
parser.add_argument('role',choices=choices,help='which role to play') | |
parser.add_argument('-p',metavar='PORT',type=int,default=1060,help='UDP port (default 1060)') | |
args = parser.parse_args() | |
function = choices[args.role] | |
function(args.p) |
When the client is started it send the server current time information and the server replies an acknowledgement saying how many bytes was received by the server. Since this program is run on the same machine and the messages are not sent via network there wont be any possible packet loss. We need to consider those aspects while writing code for real world applications.