UDP-based programming of Javafx

code show as below

  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          } the catch (IOException E1) {
 37 [              System.out.println ( "multicast address binding failed" );
 38 is          }
 39          Application.launch ();
 40      }
 41 is      @Override
 42 is      public  void Start ( S the Stage) throws Exception {
 43 is          // method TODO automatically generated stubs     
44          / * message record * / 
45          ta_1 = new new the TextArea ();
 46 is          ta_1.setPrefSize (400, 200 is );
 47         = Pane_info the ScrollPane new new the ScrollPane (ta_1);
 48          TitledPane pane_01 = new new TitledPane ( "record message" , pane_info);
 49          / * transmitting window * / 
50          ta_2 = new new the TextArea ();
 51 is          ta_2.setPrefSize (400, 100 );
 52 is          = pane_send the ScrollPane new new the ScrollPane (ta_2);
 53 is          TitledPane pane_02 = new new TitledPane ( "transmission window" , pane_send);
 54 is          / * transmitting and button events * / 
55          the button btn_send = new new the button ( "send");
 56 is          btn_send.setOnAction ( the this :: btnSend);
 57 is          / * Close and button events * / 
58          the Button btn_close = new new the Button ( "Close" );
 59          btn_close.setOnAction ( the this :: btnClose);
 60          / * button size * / 
61 is          btn_send.setPrefSize (70, 30 );
 62 is          btn_close.setPrefSize (70, 30 );
 63 is          / * position of the button * / 
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      / * Send button event * / 
80      public  void btnSend (the ActionEvent enent) {
 81          the try {
 82              String MSG = ta_2.getText ();
 83              of DatagramPacket P = new new of DatagramPacket (Msg.getBytes () , Msg.getBytes () length, IA, 6770. );
 84              socket.send (P);
 85          } catch (IOException E) {
 86              // the TODO automatically generated catch block 
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 ( "receiving failed" );
 121              }
 122          }
 123      }
 124 }

 

Results show:

Guess you like

Origin www.cnblogs.com/jdr-gbl/p/12022565.html