In: Computer Science
Like a TCP server, a UDP server's job is to set up a communication endpoint and passively waits for the client to initiate the communication; however, since UDP is connectionless, UDP communication is initiated by a datagram from the client, without going through a connection setup as in TCP.
Write a program to demonstrate the typical UDP server goes through the following four steps:
1. Construct an instance of DatagramSocket, specifying the local port and, optionally, the local address.
2. Receive an instance of DatagramPacket using the receive() method of DatagramSocket. When receive() returns, the datagram contains the client's address so we know where to send the reply.
3. Communicate by sending and receiving DatagramPackets using the send() and receive() methods of DatagramSocket.
4. When finished, deallocate the socket using the close() method of DatagramSocket.
Note: The program is very simple: it loops forever, receiving datagrams and then sending the same datagrams back to the client. Actually, your server only receives and sends back the first 255 (ECHOMAX) characters of the datagram; any excess is silently discarded by the socket implementation
Step 1 : Save the following code in ServerUDP.java
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class ServerUDP {
// for receiving data
byte[] buffer;
public ServerUDP(int port) {
DatagramSocket socket = null;
System.out.println("Server running
at port : " + port);
try {
// Create server
socket
socket = new
DatagramSocket(port);
while (true)
{
buffer = new byte[512];
DatagramPacket request = new
DatagramPacket(buffer, buffer.length);
// Receive data
socket.receive(request);
String reqMsg = new String(buffer, 0,
request.getLength());
System.out.println("Received : " +
reqMsg);
InetAddress clientAddress =
request.getAddress();
int clientPort = request.getPort();
DatagramPacket packet = new
DatagramPacket(reqMsg.getBytes(),
reqMsg.length(), clientAddress, clientPort);
// Send the data back to client
socket.send(packet);
System.out.println("Sending reply : " +
reqMsg);
}
} catch (Exception ex) {
ex.printStackTrace();
socket.close();
}
}
public static void main(String[] args) {
new ServerUDP(5000);
}
}
Step 2 : Save the following code in ClientUDP.java
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class ClientUDP {
public ClientUDP(String hostName, int port) {
DatagramSocket socket = null;
byte[] message = "Hello
World!".getBytes();
try {
// Create client
socket
socket = new
DatagramSocket();
InetAddress
address = InetAddress.getByName(hostName);
DatagramPacket
request = new DatagramPacket(
message, message.length,
address, port);
// Send data to
server
socket.send(request);
System.out.println("Sending : " + new String(message));
byte[] buffer =
new byte[512];
DatagramPacket
response = new DatagramPacket(buffer, buffer.length);
// Receive
data
socket.receive(response);
String respMsg =
new String(buffer, 0, response.getLength());
System.out.println("Received : " + respMsg);
} catch (Exception ex) {
System.out.println("Error: " + ex.getMessage());
ex.printStackTrace();
} finally {
try {
socket.close();
} catch
(Exception ex) {}
}
}
public static void main(String[] args) {
new ClientUDP("localhost",
5000);
}
}
Step 3 : Build the project and run ServerUDP.class file.
Following is my screen shot of the output from server
Step 4 : Build the project and run ClientUDP.class file.
Following is my screen shot of the output from client