UDP client/server

The UDP client/server applications are similar in structure to the structure used for TCP client/server applications. On the server side, a UDP server socket is created, which waits for client requests. The client will create a corresponding UDP socket and use it to send a message to the server. The server can then process the request and send back a response.

A UDP client/server will use the DatagramSocket class for the socket and a DatagramPacket to hold the message. There is no restriction on the message's content type. In our examples, we will be using a text message.

The UDP server application

Our server is defined next. The constructor will perform the work of the server:

public class UDPServer { public UDPServer() { System.out.println("UDP ...

Get Learning Network Programming with Java now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.