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 Server Started"); ... System.out.println("UDP Server Terminating"); } public static void main(String[] args) { new UDPServer(); } }
In the constructor's try-with-resources block, we create...