public class receivemessage {
public static void main(String[] args) throws IOException {
DatagramSocket ds = new DatagramSocket(10086);
byte[] bytes = new byte[1024];
DatagramPacket dp = new DatagramPacket(bytes,bytes.length);
while(true){
ds.receive(dp);
InetAddress address = dp.getAddress();
String hostName = address.getHostName();
String hostAddress = address.getHostAddress();
byte[] data = dp.getData();
int port = dp.getPort();
System.out.println("主机为" + hostName + "的用户在" + hostAddress + "上发送了信息:" + new String(data,0,data.length));
if(new String(data).equals("886")){
break;
}
}
ds.close();
}
}
public class sentmessage {
public static void main(String[] args) throws IOException {
DatagramSocket ds = new DatagramSocket();
Scanner sc = new Scanner(System.in);
while (true) {
String str = sc.nextLine();
byte[] bytes = str.getBytes();
String adress = "127.0.0.1";
int port = 10086;
DatagramPacket dp = new DatagramPacket(bytes,bytes.length, InetAddress.getByName(adress),port);
ds.send(dp);
if("886".equals(str)){
break;
}
}
ds.close();
}
}