Improvements to UDP Programs: Avoiding Arbitrary Servers

There are a few improvements that can easily be made to our UDP program. Let's have a look.

Problem: Replies From Arbitrary Servers

Note that at the moment, our UDP client accepts replies from any machine and assumes that it’s the one that it sent the initial message to, evident in the following line,

data, address = s.recvfrom(MAX_SIZE_BYTES) 

Note how the client does not check who it is receiving the message from. It just receives a message.

Fix with connect()

There are two quick ways to go about fixing this. The first of which is to use the connect() method to forbid other addresses from sending packets to the client.

Get hands-on with 1200+ tech skills courses.