Hello,
I need help getting this accomplished. I think I'm close but I get confused because I'm a novice at java.
I'm trying to implement a ping client that accepts the hostname and the port. I have to send packets across with the following: PING, the sequence number which is the packet numbers 0-9, the time the packet was sent and CLRF. When each packet goes over it has to wait 1 sec for a reply.
Now one of my delimnas is (1) how do I get the time appeneded to my strings and (2) how do I wait 1 sec before I send another packet. Can someone help me to figure out how to implement what I'm trying to do?
This is what I have so far:
[code]
import java.io.*;
import java.net.*;
import java.util.*;
import java.net.InetAddress.*;
/*
* Client to process ping requests over UDP.
*/
public class PingClient
{
private static final double WAIT = 0.1;
public static void main(String[] args) throws Exception
{
// Get command line argument.Make sure host and port are intialized
if (args.length != 2) {
System.out.println("Required arguments: port, host, or both");
return;
}
int port = Integer.parseInt(args[1]);
String hostname = args[0];
// Create a datagram socket for receiving and sending UDP packets
// through the port specified on the command line.
DatagramSocket socket = new DatagramSocket(port);
//Invokes DNS lookup that translates the hostname to an IP address
InetAddress IPAddress = InetAddress.getByName("hostname");
//will hold the data the client sends and receives
byte[] sendData = new byte[1024];
byte[] receiveData = new byte[1024];
//Setting up packets message
String packet0 = "PING 0";
String packet1 = "PING 1";
String packet2 = "PING 2";
String packet3 = "PING 3";
String packet4 = "PING 4";
String packet5 = "PING 5";
String packet6 = "PING 6";
String packet7 = "PING 7";
String packet8 = "PING 8";
String packet9 = "PING 9";
//converts the string into bytes
sendpacket0 = packet0.getBytes();
sendpacket2 = packet1.getBytes();
sendpacket3 = packet1.getBytes();
sendpacket4 = packet1.getBytes();
sendpacket5 = packet1.getBytes();
sendpacket6 = packet1.getBytes();
sendpacket7 = packet1.getBytes();
sendpacket8 = packet1.getBytes();
sendpacket9 = packet1.getBytes();
// Create a datagram packet to send outgoing UDP packet.
DatagramPacket request0 = new DatagramPacket(sendpacket0, 1024, PortDayTime);
DatagramPacket request1 = new DatagramPacket(sendpacket1, 1024, PortDayTime);
DatagramPacket request2 = new DatagramPacket(sendpacket2, 1024, PortDayTime);
DatagramPacket request3 = new DatagramPacket(sendpacket3, 1024, PortDayTime);
DatagramPacket request4 = new DatagramPacket(sendpacket4, 1024, PortDayTime);
DatagramPacket request5 = new DatagramPacket(sendpacket5, 1024, PortDayTime);
DatagramPacket request6 = new DatagramPacket(sendpacket6, 1024, PortDayTime);
DatagramPacket request7 = new DatagramPacket(sendpacket7, 1024, PortDayTime);
DatagramPacket request8 = new DatagramPacket(sendpacket8, 1024, PortDayTime);
DatagramPacket request9 = new DatagramPacket(sendpacket9, 1024, PortDayTime);[code]