• 大小: 612KB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2021-06-11
  • 语言: 其他
  • 标签: Socket  

资源简介

包含整个系列: Socket实战系列: Socket实战——UDP连接:https://blog.csdn.net/haoranhaoshi/article/details/86601468 Socket实战——TCP连接:https://blog.csdn.net/haoranhaoshi/article/details/86601522 Socket实战——查询数据库:https://blog.csdn.net/haoranhaoshi/article/details/86601566 Socket实战——监听数据库:https://blog.csdn.net/haoranhaoshi/article/details/86601584 Socket实战——聊天:https://blog.csdn.net/haoranhaoshi/article/details/86601771 Socket实战——文件上传:https://blog.csdn.net/haoranhaoshi/article/details/86601850

资源截图

代码片段和文件信息

package ChatByUDP;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;

import java.io.IOException;
import java.net.*;

public class AnotherChatParticipant extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        TextField textField = new TextField();

        Button button = new Button(“发送“);
        button.setTranslateX(140);
        button.setOnAction(event -> {
            String content = textField.getText();
            if(content == null || content.equals(““)){
                Alert alert = new Alert(Alert.AlertType.WARNING “请输入发送内容“ ButtonType.OK);
                alert.show();
                return;
            }

            byte[] message = content.getBytes();
            try {
                DatagramSocket datagramSocket = new DatagramSocket();
                // 数据包发往IP:127.0.0.1,端口:10002
                DatagramPacket datagramPacket = new DatagramPacket(message message.length InetAddress.getByName(“127.0.0.1“) 10002);
                datagramSocket.send(datagramPacket);
            } catch (UnknownHostException e) {
                e.printStackTrace();
            } catch (SocketException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        });

        Label label = new Label();
        label.setTranslateY(50);
        new Thread(() -> {
            try {
                // 创建IP:127.0.0.1,端口:10003的Socket监听
                DatagramSocket datagramSocket = new DatagramSocket(10003 InetAddress.getByName(“127.0.0.1“));
                while (true) {
                    byte[] receivedData = new byte[1024];
                    DatagramPacket datagramPacket = new DatagramPacket(receivedData receivedData.length);
                    datagramSocket.receive(datagramPacket);
                    // 未收到则后续不执行
                    Platform.runLater(() ->
                            label.setText(new String(datagramPacket.getData() 0 datagramPacket.getLength()))
                    );
                }
            } catch (UnknownHostException e) {
                e.printStackTrace();
            } catch (SocketException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }).start();

        primaryStage.settitle(“AnotherChatParticipant“);
        Pane pane = new Pane(textField button label);
        primaryStage.setScene(new Scene(pane 400 200));
        primaryStage.setX(500);
        primaryStage.setY(100);
        primaryStage.show();

        primaryStage.addEventHandler(WindowEvent.WINDOW_CLOSE_REQUEST

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2019-01-20 18:17  SocketTest\
     目录           0  2019-01-22 21:42  SocketTest\.idea\
     目录           0  2019-01-21 10:22  SocketTest\.idea\libraries\
     文件         288  2019-01-21 10:22  SocketTest\.idea\libraries\lib.xml
     文件         276  2019-01-19 13:49  SocketTest\.idea\misc.xml
     文件         267  2019-01-19 13:49  SocketTest\.idea\modules.xml
     文件        8915  2019-01-19 14:02  SocketTest\.idea\uiDesigner.xml
     文件       35858  2019-01-22 21:42  SocketTest\.idea\workspace.xml
     文件         495  2019-01-20 18:17  SocketTest\SocketTest.iml
     目录           0  2019-01-21 10:21  SocketTest\lib\
     文件       94360  2018-02-04 21:02  SocketTest\lib\json.jar
     文件      540852  2016-05-07 09:18  SocketTest\lib\mysql-connector-java-5.0.8-bin.jar
     目录           0  2019-01-19 14:16  SocketTest\out\
     目录           0  2019-01-19 14:16  SocketTest\out\production\
     目录           0  2019-01-22 21:30  SocketTest\out\production\SocketTest\
     目录           0  2019-01-21 16:56  SocketTest\out\production\SocketTest\ChatByUDP\
     文件        5495  2019-01-21 16:56  SocketTest\out\production\SocketTest\ChatByUDP\AnotherChatParticipant.class
     文件        5458  2019-01-21 16:56  SocketTest\out\production\SocketTest\ChatByUDP\ChatParticipant.class
     目录           0  2019-01-22 21:31  SocketTest\out\production\SocketTest\FileUploadByTCP\
     文件        5556  2019-01-22 21:30  SocketTest\out\production\SocketTest\FileUploadByTCP\FileUploadClient.class
     文件        4794  2019-01-22 21:30  SocketTest\out\production\SocketTest\FileUploadByTCP\FileUploadServer.class
     文件           4  2019-01-21 16:56  SocketTest\out\production\SocketTest\FileUploadByTCP\test1.txt
     文件           4  2019-01-22 21:31  SocketTest\out\production\SocketTest\FileUploadByTCP\test2.txt
     目录           0  2019-01-22 21:30  SocketTest\out\production\SocketTest\TCP\
     文件        5207  2019-01-22 21:30  SocketTest\out\production\SocketTest\TCP\TCPClient.class
     文件        4698  2019-01-22 21:30  SocketTest\out\production\SocketTest\TCP\TCPServer.class
     目录           0  2019-01-22 21:19  SocketTest\out\production\SocketTest\TableListenerByUDP\
     文件        7546  2019-01-22 21:19  SocketTest\out\production\SocketTest\TableListenerByUDP\ListenTableClient.class
     文件        8241  2019-01-22 21:19  SocketTest\out\production\SocketTest\TableListenerByUDP\ListenTableServer.class
     目录           0  2019-01-21 14:37  SocketTest\out\production\SocketTest\TableSelectByTCP\
     文件        5841  2019-01-21 10:47  SocketTest\out\production\SocketTest\TableSelectByTCP\SelectTableClient.class
............此处省略25个文件信息

评论

共有 条评论