Hello folks, I have a problem with my client side of UDP. I am not really good at socket programming and I hope that you dont mind helping me out with this!
The main idea of this UDP client is to send a packet and receive it back from server! It then takes the time taken from the moment the packet is sent till the moment the packet is received. The packet should be sent 10 times and then it will print each of the time for each packet! Here is my code!
import java.io.*;
import java.net.*;
import java.util.*;
public class PingClient
{
private static final double LOSS_RATE = 0.1;
private static final int AVERAGE_DELAY = 100; // milliseconds
private long startTime = -1;
private long stopTime = -1;
private boolean running = false;
public PingClient start() {
startTime = System.currentTimeMillis();
running = true;
return this;
}
public PingClient stop() {
stopTime = System.currentTimeMillis();
running = false;
return this;
}
public long getElapsedTime() {
if (startTime == -1) {
return 0;
}
if (running) {
return System.currentTimeMillis() - startTime;
}
else {
return stopTime - startTime;
}
}
public PingClient reset() {
startTime = -1;
stopTime = -1;
running = false;
return this;
}
public static void main (
String [] args) throws Exception
{
BufferedReader inFromUser = new BufferedReader (new InputStreamReader (System.in));
Random random = new Random();
// Creates a sockete for a client to connect with the PingServer
DatagramSocket clientSocket = new DatagramSocket();
// Gets the local IP Address
InetAddress IPAddress = InetAddress.getLocalHost();
byte [] sendData = new byte[1024];
byte [] receiveData = new byte [1024];
String userinput = inFromUser.readLine();
sendData = userinput.getBytes();
PingClient s = new PingClient();
// Creates a datagram socket for sending UDP packets through the port
// specified on the command line.
for (int i=10; i > 0; i--) {
s.start();
DatagramPacket sendPacket = new DatagramPacket (sendData, sendData.length, IPAddress, 8888);
clientSocket.send(sendPacket);
// Creates a datagram socket for receiving UDP packets through the port
// specified on the command line.
DatagramPacket receivePacket = new DatagramPacket (receiveData, receiveData.length);
clientSocket.receive(receivePacket);
String dat = new String(receivePacket.getData());
//System.out.println(dat);
s.stop();
System.out.println("elapsed time: " + s.getElapsedTime());// this println statement doesnt print anything??
s.reset();
// Decide whether to reply, or simulate packet loss.
if (random.nextDouble() < LOSS_RATE) {
System.out.println(" packet is lost ");
continue;
}
// Simulate network delay.
Thread.sleep((int) (random.nextDouble() * 2 * AVERAGE_DELAY));
}
}
}
import java.io.*;
import java.net.*;
import java.util.*;
/*
* Server to process ping requests over UDP.
*/
public class PingServer
{
private static final double LOSS_RATE = 0.3;
private static final int AVERAGE_DELAY = 100; // milliseconds
public static void main(String[] args) throws Exception
{
// Get command line argument.
if (args.length != 1) {
System.out.println("Required arguments: port");
return;
}
int port = Integer.parseInt(args[0]);
// Create random number generator for use in simulating
// packet loss and network delay.
Random random = new Random();
// Create a datagram socket for receiving and sending UDP packets
// through the port specified on the command line.
DatagramSocket socket = new DatagramSocket(port);
// Processing loop.
while (true) {
// Create a datagram packet to hold incomming UDP packet.
DatagramPacket request = new DatagramPacket(new byte[1024], 1024);
// Block until the host receives a UDP packet.
socket.receive(request);
// Print the recieved data.
printData(request);
// Decide whether to reply, or simulate packet loss.
if (random.nextDouble() < LOSS_RATE) {
System.out.println(" Reply not sent.");
continue;
}
// Simulate network delay.
Thread.sleep((int) (random.nextDouble() * 2 * AVERAGE_DELAY));
// Send reply.
InetAddress clientHost = request.getAddress();
int clientPort = request.getPort();
byte[] buf = request.getData();
DatagramPacket reply = new DatagramPacket(buf, buf.length, clientHost, clientPort);
socket.send(reply);
System.out.println(" Reply sent.");
}
}
/*
* Print ping data to the standard output stream.
*/
private static void printData(DatagramPacket request) throws Exception
{
// Obtain references to the packet's array of bytes.
byte[] buf = request.getData();
// Wrap the bytes in a byte array input stream,
// so that you can read the data as a stream of bytes.
ByteArrayInputStream bais = new ByteArrayInputStream(buf);
// Wrap the byte array output stream in an input stream reader,
// so you can read the data as a stream of characters.
InputStreamReader isr = new InputStreamReader(bais);
// Wrap the input stream reader in a bufferred reader,
// so you can read the character data a line at a time.
// (A line is a sequence of chars terminated by any combination of \r and \n.)
BufferedReader br = new BufferedReader(isr);
// The message data is contained in a single line, so read this line.
String line = br.readLine();
// Print host address and data received from it.
System.out.println(
"Received from " +
request.getAddress().getHostAddress() +
": " +
new String(line) );
}
}
would you be able to help me with this problem? thank you folks