Lave en ping-client til ping-server
Hej eksperter.Jeg prøver at lave en UDP Ping client til en færdiglavet UDP Ping Server
Serveren ser således ud: http://pastebin.com/sg1njCmF
Jeg har prøvet at lave en ping client, men det lykkedes ikke helt for mig.
Min ping-client ser således ud:
import java.io.*;
import java.net.*;
import java.util.*;
public class PingClient
{
public static void main(String[] args) throws Exception
{
if (args.length != 2) {
System.out.println("Required arguments: host port");
return;
}
InetAddress host = InetAddress.getByName(args[0]);
int port = Integer.parseInt(args[1]);
DatagramSocket socket = new DatagramSocket(port, host);
while (true) {
Thread.sleep(1000);
byte[] buf = new byte[256];
InetAddress address = InetAddress.getByName(args[0]);
DatagramPacket packet = new DatagramPacket(buf, buf.length, host, 2222);
socket.send(packet);
packet = new DatagramPacket(buf, buf.length);
socket.receive(packet);
String received = new String(packet.getData(), 0, packet.getLength());
System.out.println("Modtaget " + received);
}
}
}