import java.io.*;
import java.net.*;
public class TestUdpServerlong2array {
public static void main(String[] args) {
try{
DatagramSocket dtskt = new DatagramSocket(8888);
byte[] buffer = new byte[1024];
DatagramPacket dp = new DatagramPacket(buffer, buffer.length);
while(true){
dtskt.receive(dp);
ByteArrayInputStream bis = new ByteArrayInputStream(buffer);
DataInputStream dis = new DataInputStream(bis);
System.out.println(dis.readLong());
}
}catch(IOException ioe){
ioe.printStackTrace();
}
}
}
import java.net.*;
import java.io.*;
public class TestUdpClientlong2array {
public static void main(String[] args) {
long num = 1000L;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
try{
dos.writeLong(num);
byte[] ba = baos.toByteArray();
DatagramPacket dp =
new DatagramPacket(ba, ba.length, new InetSocketAddress("127.0.0.1",8888));
DatagramSocket ds = new DatagramSocket(5400);
ds.send(dp);
ds.close();
}catch(SocketException se){
se.printStackTrace();
}
catch(IOException ioe){
ioe.printStackTrace();
}
}
}
这个程序套接了流技术。算是一个先导型范例。