62,615
社区成员
发帖
与我相关
我的任务
分享
public class Client {
private static SocketAddress address;
private static String host = "127.0.0.1";
private static int port = 5678;
private static byte[] buf;
public Client() {
address = new InetSocketAddress(host, port);
}
public static void main(String args[]) {
Long l = 123456789L;
ByteArrayOutputStream bs = new ByteArrayOutputStream();
DataOutputStream data = new DataOutputStream(bs);
try {
data.writeLong(l);
buf = bs.toByteArray();
// address = new InetSocketAddress(host, port);
DatagramSocket ds = new DatagramSocket(6666);
DatagramPacket dp = new DatagramPacket(buf, buf.length, address);
ds.send(dp);
ds.close();
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class Server {
private static int port = 5678;
private static byte[] buf;
public static void main(String[] args) {
buf = new byte[1000];
DatagramPacket dp = new DatagramPacket(buf, buf.length);
ByteArrayInputStream bs = new ByteArrayInputStream(buf);
DataInputStream data = new DataInputStream(bs);
try {
DatagramSocket ds = new DatagramSocket(port);
ds.receive(dp);
System.out.println(data.readLong());
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}