62,628
社区成员
发帖
与我相关
我的任务
分享
public class UdpService {
public static void main(String[] args) throws IOException, ClassNotFoundException {
DatagramSocket udp = new DatagramSocket(8888);
while(true){
byte container[] = new byte[1024];
DatagramPacket udpdata = new DatagramPacket(container,container.length );
udp.receive(udpdata);
byte b[] = udpdata.getData();
String s = new String(b);
if(s.equals("close")){
System.out.println("connection close");
break;
}
System.out.println(s);
}
udp.close();
}
}
public class UdpClient {
public static void main(String[] args) throws IOException, ClassNotFoundException {
DatagramSocket client = new DatagramSocket(1234);
while(true){
Scanner scn= new Scanner(System.in);
System.out.print("please input(input close to close):");
String i = scn.nextLine();
byte b[] = i.getBytes();
DatagramPacket sendData = new DatagramPacket(b, b.length, new InetSocketAddress("127.0.0.1",8888));
client.send(sendData);
if(i.equals("close")){
break;
}
}
client.close();
}
}