Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Pinger time!

 
Jack Rudolf
Greenhorn
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Michael Ernest
High Plains Drifter
Sheriff
Posts: 7292
Netbeans IDE VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there -

Please have a look at our naming policy.

Thanks,

Michael
[ September 25, 2005: Message edited by: Michael Ernest ]
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So what's the problem? What does the code do? Where does it go wrong?
 
Jack Rudolf
Greenhorn
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is that the timer does not work and it does not print the time at all to the screen! I am not sure what happen to this!
When I dont see the result on the screen, I am not sure whether I got the timing correct or wrong!
 
Jack Rudolf
Greenhorn
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I have changed it to my real name already!
Cheers
 
Jack Rudolf
Greenhorn
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Ernest,
I think the timer works, but it doesnt print out the time taken to the screen at all and I put println statement there and it still did not work! Thanks for the help in advance
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic