-1

Error:

[!] Server waiting.
Cannot invoke "task2.SerializableRectangle.getX()" because "this.m_rectangle" is null
Rectangle (Object) (10.0,10.0,200.0,200.0).
Exception in thread "Server Thread" java.lang.RuntimeException: java.net.BindException: Address already in use: bind
    at task2.Server.run(Server.java:30)
    at java.base/java.lang.Thread.run(Thread.java:1589)
Caused by: java.net.BindException: Address already in use: bind
    at java.base/sun.nio.ch.Net.bind0(Native Method)
    at java.base/sun.nio.ch.Net.bind(Net.java:555)
    at java.base/sun.nio.ch.Net.bind(Net.java:544)
    at java.base/sun.nio.ch.NioSocketImpl.bind(NioSocketImpl.java:629)
    at java.base/java.net.ServerSocket.bind(ServerSocket.java:393)
    at java.base/java.net.ServerSocket.<init>(ServerSocket.java:275)
    at java.base/java.net.ServerSocket.<init>(ServerSocket.java:168)
    at task2.Server.run(Server.java:17)
    ... 1 more

Objective: I am trying to have a Server with JavaFx running and then send an serialized description for a Graphics object and have the Server draw it on JavaFx Canvas.

Server.java:

public class Server implements Runnable {
    private SerializableRectangle m_rectangle = null;
    @Override
    public void run() {
        ServerSocket server = null;
        try {
            while(true) {

                server = new ServerSocket(2345);
                System.out.println("[!] Server waiting.");

                Socket pipe = null;
                pipe = server.accept();

                ObjectInputStream serverInputStream = new ObjectInputStream(pipe.getInputStream());
                m_rectangle = (SerializableRectangle) serverInputStream.readObject();

                System.out.println(m_rectangle.toString());
            }

        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    public Rectangle getRectangle(){
        return new Rectangle(
                (int)m_rectangle.getX(),
                (int)m_rectangle.getY(),
                (int)m_rectangle.getWidth(),
                (int)m_rectangle.getHeight()
        );
    }
}

ServerApplication.java:

public class ServerApplication extends Application {
    @Override
    public void start(Stage stage) throws Exception {
        stage.setTitle("Server Application");
        Group root = new Group();
        Canvas canvas = new Canvas(300, 250);
        GraphicsContext context = canvas.getGraphicsContext2D();
        drawShape(context);
        root.getChildren().add(canvas);
        stage.setScene(new Scene(root));
        stage.show();
    }

    private void drawShape(GraphicsContext context)
    {
        try{
            Server server = new Server();
            Thread serverThread = (new Thread(server));
            serverThread.setName("Server Thread");
            serverThread.setDaemon(true);
            serverThread.start();

            Rectangle rect = server.getRectangle();
            context.fillRect(rect.getX(), rect.getY(), rect.getWidth(), rect.getHeight());
            context.setStroke(Color.BLUE);

        }catch (Exception ex){
            System.out.println(ex.getMessage());
        }
    }

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

Client.java:

public class Client {
    public static void main(String[] args) {
        Socket socket = null;
        try {
            socket = new Socket(InetAddress.getLocalHost(), 2345);

            ObjectOutputStream clientOutputStream = new ObjectOutputStream(socket.getOutputStream());
            clientOutputStream.writeObject(new SerializableRectangle(10, 10, 200, 200));
            clientOutputStream.close();

        } catch (UnknownHostException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
Alix Blaine
  • 585
  • 2
  • 16
  • 1
    The error message means that a port that the java wants to listen to is already being listened to. I suspect that it is the port 2345. The following needs to come out of the while(true) loop: server = new ServerSocket(2345). Alternatively you unintentionally have another instance of this process running already. – John Williams Mar 28 '23 at 18:25
  • Good catch! I missed that. – Queeg Mar 28 '23 at 18:28

1 Answers1

0

Your log shows two different exceptions (NullPointerException and BindException), which might be directly related or not.

For the BindException, check what process is occupying the address you intend to use. Very likely it is your own server, but you can verify by following How do I find out which process is listening on a TCP or UDP port on Windows? or What processes are using which ports on unix?

For the NullPointerException check the stack trace (maybe you can improve your code to provide one) to find out where in your source code the reference gets resolved and work around that. Just by looking at your code I guess the NullPointerException occurs at Server.getRectangle() as it is getting called before a rectangle was received via the network.

Queeg
  • 7,748
  • 1
  • 16
  • 42