JavaFX Socket Handling

To try to illustrate the situation:

I created the SocketManager class. This extends the JavaFX service and is designed to create new threads (the JavaFX task that I expanded and called SocketListener), which handles specific socket connections.

Inside SocketListener, I have a loop that listens for input through a socket (and returns a response to the client).

Problem : The loop inside the SocketListener is blocked (obviously). This means that I never achieve a “return” inside the Socket Listener invocation method (which extends the JavaFX Task). I am curious about which template to implement so that I can return a value from SocketListener without breaking my loop, which maintains a client connection. One of the ideas that came to mind was to create a special event to trigger when each message got through the socket, but I want to make sure that everything that I implement is not just “working”, but also optimal.

Here is the code for the two classes I am referring to:

import javafx.concurrent.Service;
import javafx.concurrent.Task;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * The primary class for listening for and issuing communication over sockets in VDTS
 * applications.
 * WARNING: This class is blocking, and should be run in a separate thread.
 */
public class SocketManager extends Service<SocketMessage> {

    /**
     * The port that SocketManager instance will listen on.
     */
    int listenPort;

    /**
     * The ServerSocket instance used by SocketManager for receiving socket connections.
     */
    ServerSocket server;

    /**
     * Constructs a SocketManager and begins listening on the given port.
     * @param listenPort The port that the SocketManager instance will listen on.
     */
    public SocketManager(int listenPort){
        this.listenPort = listenPort;
        try {
            this.server = new ServerSocket(listenPort);
        } catch (IOException ex) {
            System.out.println(ex.getMessage());
            //TODO
        }
    }

    /**
     * Returns the port currently being listened to by SocketManager instance.
     * @return the port being listened to.
     */
    public int getListenPort() {
        return listenPort;
    }

    /**
     * Sets a new port for the SocketManager instance to listen to. This will destroy
     * the current ServerSocket, ending any communication via the old instance. Use
     * with caution.
     * @param listenPort The new port this SocketManager instance will listen on.
     */
    public void setListenPort(int listenPort) {
        this.listenPort = listenPort;
    }

    @Override
    protected Task createTask() {
        String returnedMessage;
        Socket client;
        SocketListener messenger;
        while(true){
            try {
                client = null;
                client = server.accept();
                messenger = new SocketListener(client);
                new Thread(messenger).start();
            } catch (IOException ex) {
                System.out.println(ex.getMessage());
                //TODO
            }
        }
    }
}

And another

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

/**
 * An extension on javafx.concurrency.Task.
 * SocketListener is responsible for retrieving a message (string) from a client over
 * the socket and returning this string (Usually to a SocketManager).
 */
public class SocketListener extends javafx.concurrent.Task<String> {

    /**
     * Client socket which is referenced for persistent communication.
     */
    private final Socket client;

    /**
     * PrintWriter used to write to client.
     */
    private PrintWriter clientOut;

    //TODO This is a temporary property for testing, remove it;
    private int messageCount = 0;

    /**
     * Constructs a SocketListener given a client (Socket)
     * @param client
     */
    public SocketListener(Socket client){
        this.client = client;
        try {
            this.clientOut = new PrintWriter(client.getOutputStream(), true);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * Listens for and returns the value of the message received over socket.
     * @return The message received from the client.
     * @throws Exception
     */
    @Override
    protected String call() throws Exception {
        String nextLine = null;
        BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
        //TODO remove in production should not require highfive
        clientOut.println("Just saying hi.");
        while ((nextLine = in.readLine()) != null) {
            this.messageCount++;
            clientOut.println("You've messaged me " + this.messageCount + " times.");
        }
        return "Socket closed.";
    }
}
+4
source share
1 answer

? (java.net). Java NIO, .

NIO , , , . , , , . , , - , , , .

JavaFX - , . , JavaFX (, Swing) , Task, Service, , runLater Platform.

, , , , NIO JavaFX. , , , JavaFX Application Thread. JavaFX, JavaFX Application Thread . .;)

NIO , , "" , , Apache MINA project NETTY framework. , NIO, , .

, . . (

+1

All Articles