Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a UDP client program that sends and receives datagrams. It will have to se

ID: 3724276 • Letter: W

Question

Write a UDP client program that sends and receives datagrams. It will have to send and receive, with these actions being asynchronous. This can be done with threads or with non-blocking I/O.

Assume that there is a server c program that is going to "echo" the message back to your UDP client c program

In the main function Get time now, put in message, do sendto

In the thread use the recvfrom, to extract the time and Get time now, Extract time sent from message received, compute delta time, accumulate total delta times,increment num_received .

In the main function Wait for any straggler replies, Compute ave RTT, compute message loss percentage

If the main thread sleeps for 2 seconds, any straggler replies will have time to turn up and be seen by the recvfrom thread

Explanation / Answer

import java.io.*;
import java.net.*;

/**
* This program demonstrates how to implement a UDP client program.
*/
public class QuoteClient {

public static void main(String[] args) {
if (args.length < 2) {
System.out.println("Syntax: QuoteClient <hostname> <port>");
return;
}

String hostname = args[0];
int port = Integer.parseInt(args[1]);

try {
InetAddress address = InetAddress.getByName(hostname);
DatagramSocket socket = new DatagramSocket();

while (true) {

DatagramPacket request = new DatagramPacket(new byte[1], 1, address, port);
socket.send(request);

byte[] buffer = new byte[512];
DatagramPacket response = new DatagramPacket(buffer, buffer.length);
socket.receive(response);

String quote = new String(buffer, 0, response.getLength());

System.out.println(quote);
System.out.println();

Thread.sleep(10000);
}

} catch (SocketTimeoutException ex) {
System.out.println("Timeout error: " + ex.getMessage());
ex.printStackTrace();
} catch (IOException ex) {
System.out.println("Client error: " + ex.getMessage());
ex.printStackTrace();
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}

UDP server program

import java.io.*;
import java.net.*;
import java.util.*;

/**
* This program demonstrates how to implement a UDP server program.
*
*
* @author www.codejava.net
*/
public class QuoteServer {
private DatagramSocket socket;
private List<String> listQuotes = new ArrayList<String>();
private Random random;

public QuoteServer(int port) throws SocketException {
socket = new DatagramSocket(port);
random = new Random();
}

public static void main(String[] args) {
if (args.length < 2) {
System.out.println("Syntax: QuoteServer <file> <port>");
return;
}

String quoteFile = args[0];
int port = Integer.parseInt(args[1]);

try {
QuoteServer server = new QuoteServer(port);
server.loadQuotesFromFile(quoteFile);
server.service();
} catch (SocketException ex) {
System.out.println("Socket error: " + ex.getMessage());
} catch (IOException ex) {
System.out.println("I/O error: " + ex.getMessage());
}
}

private void service() throws IOException {
while (true) {
DatagramPacket request = new DatagramPacket(new byte[1], 1);
socket.receive(request);

String quote = getRandomQuote();
byte[] buffer = quote.getBytes();

InetAddress clientAddress = request.getAddress();
int clientPort = request.getPort();

DatagramPacket response = new DatagramPacket(buffer, buffer.length, clientAddress, clientPort);
socket.send(response);
}
}

private void loadQuotesFromFile(String quoteFile) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader(quoteFile));
String aQuote;

while ((aQuote = reader.readLine()) != null) {
listQuotes.add(aQuote);
}

reader.close();
}

private String getRandomQuote() {
int randomIndex = random.nextInt(listQuotes.size());
String randomQuote = listQuotes.get(randomIndex);
return randomQuote;
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote