UDPClientpublic class UDPClient extends Object This class allows you to send and receive data via UDP
without concerning yourself with DatagramPackets
and DatagramSockets. |
Fields Summary |
---|
private InetAddress | remote | private int | port | private DatagramSocket | ds | private byte[] | receiveBuffer |
Constructors Summary |
---|
public UDPClient(InetAddress remoteHost, int remotePort, int timeout)Creates a new UDPClient.
this.remote = remoteHost;
this.port = remotePort;
ds = new DatagramSocket();
// the next lines require Java 2
ds.connect(remote, port);
ds.setSoTimeout(timeout);
| public UDPClient(InetAddress remoteHost, int remotePort)
this.remote = remoteHost;
this.port = remotePort;
ds = new DatagramSocket();
// the next line requires Java 2
ds.connect(remote, port);
| public UDPClient(String hostname, int port)
this(InetAddress.getByName(hostname), port);
|
Methods Summary |
---|
public java.net.InetAddress | getAddress()
return this.remote;
| public int | getLocalPort()
return this.ds.getLocalPort();
| public int | getPort()
return this.port;
| public static void | main(java.lang.String[] args)
String hostname = "localhost";
int port = 7;
if (args.length > 0) {
hostname = args[0];
}
if (args.length > 1) {
try {
port = Integer.parseInt(args[1]);
}
catch (Exception e) {}
}
try {
InetAddress ia = InetAddress.getByName(hostname);
UDPClient client = new UDPClient(ia, port);
Random r = new Random();
for (int i = 1; i <= 8192; i++) {
try {
byte[] data = new byte[i];
r.nextBytes(data);
client.send(data);
byte[] result = client.receive();
if (result.length != data.length) {
System.err.println("Packet " + i
+ " failed; input length: " + data.length + "; output length: " + result.length);
continue;
}
for (int j = 0; j < result.length; j++) {
if (data[j] != result[j]) {
System.err.println("Packet " + i + " failed; data mismatch");
break;
}
}
}
catch (IOException e) {
System.err.println("Packet " + i + " failed with " + e);
}
}
}
catch (UnknownHostException e) {
System.err.println(e);
}
catch (SocketException se) {
System.err.println(se);
}
| public byte[] | receive()This method blocks until a UDP Datagram is received.
This can be an indefinite amount of time if
the host is unreachable so calls to this method should
be placed in a separate thread from the main program.
DatagramPacket incoming = new DatagramPacket(receiveBuffer, receiveBuffer.length);
ds.receive(incoming);
byte[] result = new byte[incoming.getLength()];
System.arraycopy(incoming.getData(), 0, result, 0, incoming.getLength());
return result;
| public void | send(byte[] data)This method sends data to the remote host via UDP. If the array
is longer than the maximum reliable length of a UDP Datagram
(8192) bytes then an IOException is thrown
if (data.length > 8192) throw new IOException("Too much data");
DatagramPacket dp
= new DatagramPacket(data, data.length, remote, port);
ds.send(dp);
| public void | send()This method sends an empty datagram to the remote host via UDP.
byte[] b = new byte[1];
this.send(b);
| public java.lang.String | toString()
return "[UDPClient:address=" + remote + ";port=" + port + "]";
|
|