Question
Problem 2 Change the client and server so that you can specify the buffer size as a command-line parameter. You should also be able to
Problem 2
Change the client and server so that you can specify the buffer size as a command-line parameter. You should also be able to specify a message transfer rate for the client. The buffer size is given in bytes and the transfer rate in messages per second. If the transfer rate is 0, the client should send a single message once.
Update the code to send messages according to the transfer rate. You do not need to space out messages over the second; you can send all messages and then wait until the second is over. Also, note that it should keep sending messages (at the configured rate) until you abort the client.
Use a transfer rate of 5 messages per second and let it run for (at least) 5 seconds. Take a screenshot and include it in the report. You can use the default buffer size.
Please modified the Client code
This is the following UDPEchoClient.java
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
public class UDPEchoClient {
public static final int BUFSIZE= 1024;
public static final int MYPORT= 0;
public static final String MSG= "An Echo Message!";
public static void main(String[] args) throws IOException {
byte[] buf= new byte[BUFSIZE];
if (args.length != 2) {
System.err.printf("usage: %s server_name port ", args[1]);
System.exit(1);
}
/* Create socket */
DatagramSocket socket= new DatagramSocket(null);
/* Create local endpoint using bind() */
SocketAddress localBindPoint= new InetSocketAddress(MYPORT);
socket.bind(localBindPoint);
/* Create remote endpoint */
SocketAddress remoteBindPoint=
new InetSocketAddress(args[0],
Integer.valueOf(args[1]));
/* Create datagram packet for sending message */
DatagramPacket sendPacket=
new DatagramPacket(MSG.getBytes(),
MSG.length(),
remoteBindPoint);
/* Create datagram packet for receiving echoed message */
DatagramPacket receivePacket= new DatagramPacket(buf, buf.length);
/* Send and receive message*/
socket.send(sendPacket);
socket.receive(receivePacket);
/* Compare sent and received message */
String receivedString=
new String(receivePacket.getData(),
receivePacket.getOffset(),
receivePacket.getLength());
if (receivedString.compareTo(MSG) == 0)
System.out.printf("%d bytes sent and received ", receivePacket.getLength());
else
System.out.printf("Sent and received msg not equal! ");
socket.close();
}
}
This is the program of ServerProgramme
/*
UDPEchoServer.java
A simple echo server with no error handling
*/
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
public class UDPEchoServer {
public static final int BUFSIZE= 1024;
public static final int MYPORT= 4950;
public static void main(String[] args) throws IOException {
byte[] buf= new byte[BUFSIZE];
/* Create socket */
DatagramSocket socket= new DatagramSocket(null);
/* Create local bind point */
SocketAddress localBindPoint= new InetSocketAddress(MYPORT);
socket.bind(localBindPoint);
while (true) {
/* Create datagram packet for receiving message */
DatagramPacket receivePacket= new DatagramPacket(buf, buf.length);
/* Receiving message */
socket.receive(receivePacket);
/* Create datagram packet for sending message */
DatagramPacket sendPacket=
new DatagramPacket(receivePacket.getData(),
receivePacket.getLength(),
receivePacket.getAddress(),
receivePacket.getPort());
/* Send message*/
socket.send(sendPacket);
System.out.printf("UDP echo request from %s", receivePacket.getAddress().getHostAddress());
System.out.printf(" using port %d ", receivePacket.getPort());
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started