An Overview of UDP...

What is UDP?

How can we use it in Delphi?

An Introduction to UDP



UDP is an abbreviation for User Datagram Protocol. It's nothing but a connectionless transport protocol that runs on TCP/IP's IP.



The advantage of this protocol is that it is connectionless. It doesn't need any connection before sending data packets to another computer.



The disadvantage is that it provides an unreliable datagram service. That is, the data packets may be duplicated, lost or received in a different order than the one in which they were sent. So the application must handle all those situations robustly.



The receiving program requests a number of bytes (the maximum will be the total number of bytes in the received packet). If less than the full packet is read, then the remainder is discarded. Then the next read is from the next packet. That means the boundaries of the original packet are preserved. For that the application must handle error correction while reading packets.



This UDP is best suited for small, independent requests like requesting a value of a variable etc., If the data is too large to send (i.e many packets of data) and valuable, then UDP is not the preferred protocol to use.



There is a component in Delphi 5 edition for the UDP from NetMasters called NMUDP. That component is similar to use as TclientSocket component.



The comparison of TClientSocket component with TNMUDP component:



As far as the properties are concerned, here in NMUDP we need to set the LocalPort(it could be any integer greater than zero; but should not be zero) to receive the data sent from the server in addition to the RemoteHost and RemotePort. But actually, the host may not be a remote one. It could be a local machine. (i.e) we can send data packets to the client machine itself and get response back for the testing purposes.



Also we can set the Report Level property to get the status during the transmission.



And as far as the methods are concerned, there is no major difference; you have the ReadStream, ReadBuffer methods as in TClientSocket component.



Regarding the events:



As the event Onclientsocketread in Tclientsocket component, here we can use the key event OnDataReceived to get the data back from the server.



Regarding the boundaries of the data packets, we need to identify the boundaries of the data packets while using either the TclientSocket or the NMUDP component to get the exact data sent from the server.



For that (irrespective of which component you use), we can use the concept of message header tag and end tag like HTML tags. By that we can identify the starting and ending of a data packet. Also we can send many information in a data packet with different message heading/ending tag.

(This paragraph will answer a question a person asked sometime back thro' e-mail)



In my application, I'm sending a whole bunch of bytes to another computer and getting the response back using the message header/end tag only. With this approach, there is very less possibility of losing data. If we didn't get the whole tag contents between the header/end tag, we can throw an error to the user so that the user can try resending the same data again or take some other steps robustly.



Enjoy!!!



Thanks.

Magesh.


 

Share this article!

Follow us!

Find more helpful articles: