【问题标题】:How to have network capabilities for a Java application?如何为 Java 应用程序提供网络功能?
【发布时间】:2014-02-19 15:38:20
【问题描述】:

我有一个 Java 应用程序,它可以输入图像,提供许多选项来编辑它们(裁剪、调整大小),然后将它们导出到本地存储。

我目前尝试使用 java.net 将服务器附加到此应用程序,但遇到了几个问题,因为我以前从未这样做过。

  1. 我在 GUI 上有一个激活服务器的按钮,因此应用程序可以接收命令 - 问题是服务器使用了一个 while 循环,它在与客户端断开连接之前不会停止,这意味着程序不能跑到那时。我假设它需要创建一个新线程,但是如何在不创建新实例的情况下向应用发送命令?
  2. 我想通过服务器将图像从客户端发送到应用程序。客户端当前使用 JFileChooser 选择图像,但我如何将其发送到流中并在另一端重新创建?

我的代码只是client 和server 的修改版本。它只是删除了回显功能并根据客户端发送的字符串运行一个方法。 (例如,客户端向服务器发送“open” - 服务器运行 open(){} 方法。

编辑: 代码:

这是服务器代码。 ImageGUI 是包含所有命令的 GUI,并包含 openImage 方法。 私有 int portNumber = 8888;

public Server() {

    try (
    // Create Server socket
    ServerSocket theServer = new ServerSocket(portNumber);
            // Create socket for client
            Socket clientSocket = theServer.accept();
            // Use to write to client
            PrintWriter out = new PrintWriter(
                    clientSocket.getOutputStream(), true);
            // Used to read from client
            BufferedReader in = new BufferedReader(new InputStreamReader(
                    clientSocket.getInputStream()));) {
        // Waits for Client commands
        String clientCommand;
        while ((clientCommand = in.readLine()) != null) {
            if (clientCommand.contains("open")) {
                // Runs open method in ImageGUI
                new ImageGUI().openImage(); // I don't want this because it
                                            // will create another GUI
                                            // I want to run the openImage()
                                            // in the GUI already open
            }
        }

    } catch (IOException e) {
        e.printStackTrace();
    }

}

【问题讨论】:

  • but how do I send commands to the app without creating a new instance 嗯?
  • 我在代码中添加了注释来解释我的意思。

标签: java multithreading servlets thread-safety


【解决方案1】:

您可能确实想在自己的线程中运行服务器的 accept() 循环。

您的服务器 GUI 的“启动服务器”按钮将启动此线程。确保您还有一个“停止服务器”按钮,它告诉服务器线程退出。当用户退出应用程序时,还要告诉服务器线程退出。

您需要决定发送图像的协议。

这可能很简单,就像将图像文件中的字节推入套接字,然后关闭套接字 - 或者它可能更复杂,比如将字节块包装在标头中,说明数据包有多大,什么它是为了等等。

或者它可能是其他人发明的预先存在的协议 - 在这种情况下,您可以使用库实现。许多人会为此目的使用 HTTP。

【讨论】:

  • 谢谢,我还有一些后续问题。图像以 OpenCV 的 Mat 格式编辑,但使用 BufferedImages 显示,您推荐哪种传输图像的方法?如何让按钮停止服务器?
  • 当按钮被点击时,调用serverThread.stop()。任何体面的服务器实现示例都会有一个 stop() 方法。
  • 至于从 BufferedImage 获取要写入网络的内容 - 使用与写入文件相同的方法,但不要使用 FileOutputStream,而是写入您选择的任何协议。
  • 谢谢,所以我需要先初始化线程,然后启动它并在单击按钮时停止它。因为我是在按下按钮时创建的
  • 什么时候初始化没关系,只要在启动之前就行!
【解决方案2】:
  1. 您可以使用共享的BlockingQueue 发送和使用命令。

    BlockingQueue 是线程安全的。使用take() 让您的服务器等待下一个命令,并在主线程中使用add() 将命令添加到队列中。

    然后您可以使用类、枚举甚至字符串来表示命令,并将它们从您的主线程发送到您的服务器线程。

  2. 要发送图像,请使用来自客户端 (echoSocket.getOutputStream()) 的 OutputStream 写入数据。服务器端,使用InputStream(clientSocket.getInputStream())接收数据。

    例如:

    // Client side
    int returnVal = chooser.showOpenDialog(parent);
    if(returnVal == JFileChooser.APPROVE_OPTION) {
        File imageFile = chooser.getSelectedFile();
    
        try(FileInputStream imageInputStream = new FileInputStream(imageFile);
            OutputStream serverOutputStream = echoSocket.getOutputStream()) {
            int[] buffer = new int[1024];
            int readCount = imageInputStream.read(buffer);
            while(readCount > 0) {
                serverOutputStream.write(buffer, 0, readCount);
                readCount = imageInputStream.read(buffer);
            }
        }
    }
    

    还有:

    // Server side
    File folder = new File("/tmp/");
    File imageFile = new File(folder, "image.jpg");
    
    try(InputStream clientInputStream = clientSocket.getInputStream();
            FileOutputStream imageOutputStream = new FileOutputStream(imageFile)) {
            int[] buffer = new int[1024];
            int readCount = clientInputStream.read(buffer);
            while(readCount > 0) {
                imageOutputStream.write(buffer, 0, readCount);
                readCount = clientInputStream.read(buffer);
            }
        }
    }
    

【讨论】:

    猜你喜欢
    • 2012-01-20
    • 1970-01-01
    • 1970-01-01
    • 2020-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多