Question

In: Computer Science

Like a TCP server, a UDP server's job is to set up a communication endpoint and...

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

Solutions

Expert Solution

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


Related Solutions

Research on TCP, UDP, and IP. And Define TCP, UDP, and IP Compare the header of...
Research on TCP, UDP, and IP. And Define TCP, UDP, and IP Compare the header of TCP, UDP, and IP packets. Why are there more fields in PCP header than UDP and IP?
Compare and contrast TCP and UDP.
Compare and contrast TCP and UDP.
What is UDP and how does it work? What are the differences between TCP and UDP?....
What is UDP and how does it work? What are the differences between TCP and UDP?. Provide examples (if applicable) to support your comparative review.
Consider the TCP connection mechanism.    i. What mechanism is used to set up a TCP...
Consider the TCP connection mechanism.    i. What mechanism is used to set up a TCP connection? ii. Why is the mechanism needed? iii. Outline how the mechanism works with a diagram. iv.CLASSFULL AND CLASSLESS IP
For a Linux server: • how to Set up a print server (CUPS), allow clients to...
For a Linux server: • how to Set up a print server (CUPS), allow clients to connect, print to PDF • Create a script that backs up the printed documents every 6 hours please can you help me
What are the services provided by TCP?  What services are provided by UDP?
What are the services provided by TCP?  What services are provided by UDP?
What are the differences between TCP and UDP? What are transport protocols?
What are the differences between TCP and UDP? What are transport protocols?
1. Consider running real-time traffic, like VoIP, over a TCP or UDP socket. Why might you...
1. Consider running real-time traffic, like VoIP, over a TCP or UDP socket. Why might you prefer to run such an application using a UDP transport layer? 2. Consider running real-time traffic, like VoIP, over a TCP or UDP socket. Suppose you chose to use TCP for your application; what would you need to do to ensure that the robust transport algorithm does not harm real-time performance?
Using node.js, create the following tasks. 1. Set up a server and HTML file server as...
Using node.js, create the following tasks. 1. Set up a server and HTML file server as shown in the videos. Once you have it successfully running, make the following adjustments A. When a 404 error (file not found) occurs, display a funny message about the file missing and/or did you forget how to type? B. If the user enters a request for the home page (index.html) then: Display an index.html page you have created which includes your name, course number,...
Suppose a TCP client needs to send 3 packets to the TCP server. Before sending the...
Suppose a TCP client needs to send 3 packets to the TCP server. Before sending the first packet, the estimated RTT is 50 ms, and the estimated deviation of the sample RTT is 10 ms. The parameters α= 0.1, and β = 0.2. The measured sample RTT for the three packets are 60ms, 70 ms, and 40 ms, respectively. Please compute the time out value that was set for each packet right after it is being transmitted out.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT