代码如下
1 package 测试; 2 3 import java.io.IOException; 4 import java.net.DatagramPacket; 5 import java.net.InetAddress; 6 import java.net.MulticastSocket; 7 import java.text.SimpleDateFormat; 8 import java.util.Date; 9 import java.util.Optional; 10 11 import javafx.application.Application; 12 import javafx.event.ActionEvent; 13 import javafx.scene.Scene; 14 import javafx.scene.control.Alert; 15 import javafx.scene.control.Button; 16 import javafx.scene.control.ButtonType; 17 import javafx.scene.control.ScrollPane; 18 import javafx.scene.control.TextArea; 19 import javafx.scene.control.TitledPane; 20 import javafx.scene.layout.AnchorPane; 21 import javafx.scene.layout.VBox; 22 import javafx.stage.Stage; 23 24 public class FX_UDP extends Application{ 25 static InetAddress ia; 26 static MulticastSocket socket; 27 static TextArea ta_1,ta_2; 28 public static void main(String[] args) { 29 try { 30 ia = InetAddress.getByName("228.9.6.7"); 31 socket = new MulticastSocket(6770); 32 socket.joinGroup(ia); 33 FX_UDP fu = new FX_UDP(); 34 Private_recive c = fu.new Private_recive(socket); 35 c.start(); 36 } catch (IOException e1) { 37 System.out.println("组播地址绑定失败"); 38 } 39 Application.launch(); 40 } 41 @Override 42 public void start(Stage s) throws Exception { 43 // TODO 自动生成的方法存根 44 /*消息记录*/ 45 ta_1 = new TextArea(); 46 ta_1.setPrefSize(400, 200); 47 ScrollPane pane_info = new ScrollPane(ta_1); 48 TitledPane pane_01 = new TitledPane("消息记录", pane_info); 49 /*发送窗口*/ 50 ta_2 = new TextArea(); 51 ta_2.setPrefSize(400, 100); 52 ScrollPane pane_send = new ScrollPane(ta_2); 53 TitledPane pane_02 = new TitledPane("发送窗口", pane_send); 54 /*发送和按钮事件*/ 55 Button btn_send = new Button("发送"); 56 btn_send.setOnAction(this::btnSend); 57 /*关闭和按钮事件*/ 58 Button btn_close = new Button("关闭"); 59 btn_close.setOnAction(this::btnClose); 60 /*按钮大小*/ 61 btn_send.setPrefSize(70, 30); 62 btn_close.setPrefSize(70, 30); 63 /*按钮的位置*/ 64 AnchorPane.setTopAnchor(btn_close, 350.0); 65 AnchorPane.setLeftAnchor(btn_close, 100.0); 66 AnchorPane.setTopAnchor(btn_send, 350.0); 67 AnchorPane.setLeftAnchor(btn_send, 200.0); 68 69 VBox vb= new VBox(pane_01,pane_02); 70 71 AnchorPane apane = new AnchorPane(vb,btn_send,btn_close); 72 73 Scene scene = new Scene(apane); 74 s.setScene(scene); 75 s.setHeight(500); 76 s.setWidth(420); 77 s.show(); 78 } 79 /*发送按钮事件*/ 80 public void btnSend(ActionEvent enent){ 81 try { 82 String msg = ta_2.getText(); 83 DatagramPacket p = new DatagramPacket(msg.getBytes(), msg.getBytes().length, ia, 6770); 84 socket.send(p); 85 } catch (IOException e) { 86 // TODO 自动生成的 catch 块 87 e.printStackTrace(); 88 } 89 } 90 public void btnClose(ActionEvent event){ 91 Alert alert = new Alert(Alert.AlertType.CONFIRMATION); 92 alert.setTitle("退出"); 93 alert.setHeaderText("你是否要退出"); 94 Optional<ButtonType> result = alert.showAndWait(); 95 if (result.get() == ButtonType.OK) { 96 System.exit(0); 97 } else { 98 event.consume(); 99 } 100 } 101 class Private_recive extends Thread { 102 MulticastSocket socket; 103 public Private_recive(MulticastSocket s) { 104 this.socket = s; 105 } 106 @Override 107 public void run() { 108 byte[] buf = new byte[100]; 109 DatagramPacket recv = new DatagramPacket(buf, buf.length); 110 Date day = new Date(); 111 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 112 try { 113 while (true) { 114 socket.receive(recv); 115 String str = new String(recv.getData(), 0, recv.getLength()); 116 ta_1.appendText(df.format(day) + "\n"); 117 ta_1.appendText(str + "\n"); 118 } 119 } catch (IOException e1) { 120 System.out.println("接受失败"); 121 } 122 } 123 } 124 }
结果验证: