Name: yyT116575 Date: 02/05/2001
java version "1.3.0"
Java(TM) 2 Runtime Environment. Standard Edition (build 1.3.0-c)
Java HotSpot(TM) Client VM (build 1.3.0-C.mixed mode)
1. byte[] getData()in java.net.DatagramPacket returns the current packet data
along with the old packet information.
2. The sample Server and Client programs are,
//Server Program starts here
import java.net.*;
import java.io.*;
class TestServer {
public static void main(String args[]) throws Exception {
DatagramSocket ds = new DatagramSocket(6000);
byte b[] = new byte[100];
DatagramPacket dp = new DatagramPacket(b,b.length);
while(true) {
System.out.println("Waiting...");
ds.receive(dp);
String reply = "Hello Mr/Ms."+new String(dp.getData());
ds.send(new DatagramPacket(reply.getBytes(),reply.length(),dp.getAddress(),dp.getPort()));
}
}
}
//Server Program ends here
//Client Program starts here
import java.net.*;
class TestClient {
public static void main(String args[]) throws Exception {
String msg = args[0];
DatagramSocket ds = new DatagramSocket();
byte b[] = new byte[100];
DatagramPacket dp = new DatagramPacket(b,b.length);
ds.send(new DatagramPacket(msg.getBytes(),msg.length(),InetAddress.getByName("LocalHost"),6000));
ds.receive(dp);
System.out.println("Reply : "+new String(dp.getData()));
}
}
//Client Program ends here
The output in the client side will be as
----------------------------------------
(a) For the first time,
>java TestClient HelloWorld <Enter>
Reply : Hello Mr/Ms.HelloWorld
(b) For the second time,
>java TestClient Java <Enter>
Reply : Hello Mr/Ms.JavaoWorld
------//The extra old data is also returned by the getData() method of DatagramPacket.
(Review ID: 116279)
======================================================================
- relates to
-
JDK-4424096 DatagramPacket spec needs clarification
-
- Resolved
-