I am doing my homework and my professor said I need to establish a connection on UDP which I can not understand why do this because I thought UDP doesn't need to establish a connection.
Also, I need to disconnect when the client received all data from the server. Then I need to reconnect to send data to the server again.
I'm using python and I wonder if I write code like this
Client side
client.close() //is it right to disconnect?
client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) // is it right to reconnect?
Am I following right direction that professor expects?
CodePudding user response:
You're right, UDP doesn't have connections in the normal sense, so it's hard to know what exactly your professor had in mind. The best course of action therefore would be to just ask them.
But if I had to guess, they either want you to:
- call
connect()
on the socket or - establish some sort of "logical" connection at the application level.
Calling connect()
on a UDP socket doesn't do any actual I/O, but it does tell the socket who the destination is. This allows you to send data using send()
as opposed to sendto()
.
If your prof meant option (2), then it's probably just about having an information exchange between two endpoints. So although there's no connection at the protocol layer, the two endpoints talk to each other and are therefore connected.