How to terminate socket within an infinite loop?

Asked

Viewed 209 times

0

Hello, I created a server-client project using a socket that listens on the port and sends the element of a "Queue" until the queue has no more elements. However, when I close the client application, the server remains open and listening. The problem is that I will have to insert this program into another application and even if I close the application, the door will still be open and listening. What should I do to fix this?

Here is the code:

Server:

public class Server {

    public static void main(String[] args) {
        Connection conn = new Connection();
        new Thread(conn).start();
    }

    private static class Connection implements Runnable {
        Queue<MovementCommand> commandQueue = new LinkedList<>();

        @Override
        public void run() {
            try (ServerSocket serverSocket = new ServerSocket(5005)) {
                Socket socket = serverSocket.accept();

                listener(socket);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

        private void listener(Socket socket) {
            commandQueue.add(new MovementCommand("Point-0001", "NOP"));
            commandQueue.add(new MovementCommand("Point-0002", "NOP"));
            commandQueue.add(new MovementCommand("Point-0004", "NOP"));
            commandQueue.add(new MovementCommand("Point-0008", "NOP"));
            commandQueue.add(new MovementCommand("Point-0011", "NOP"));

            boolean alive = true;
            MovementCommand curCommand;

            while (alive) {
                try {
                     DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream());
                     DataInputStream inputStream = new DataInputStream(socket.getInputStream());
                     curCommand = this.commandQueue.poll();

                    if (curCommand != null) {
                       outputStream.writeUTF(curCommand.getPoint() + " - " + curCommand.getOperation());

                       String data = inputStream.readUTF();
                       System.out.println("New reported position: " + data);
                    }
                } catch (IOException ex) {
                    ex.printStackTrace();
                    alive = false;
                }
            }
        }
    }
}

Client:

public class Client {
    public static void main(String[] args) {
        try (Socket socket = new Socket("localhost", 5005)) {
            DataInputStream inputStream = new DataInputStream(socket.getInputStream());
            DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream());

            boolean alive = true;

            while (alive) {
                try {
                    String data = inputStream.readUTF();
                    System.out.println("Incoming data: " + data);

                    Thread.sleep(2000);

                    outputStream.writeUTF(data.substring(0, 10));
                    outputStream.flush();
                } catch (Exception ex) {
                    ex.printStackTrace();
                    alive = false;
                }
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

Grateful from now on!!

  • What it is and what it’s for curCommand?

  • It is the current command that the server has to send. The commands are queued and it sends the next one when the client confirms that it has received it. It’s just for example, because it’s for a robot control project

  • And how does the server know that there is a client connected? Or does it not know?

  • I think the server assumes that it has a client connected when it runs the method serverSocket.accept();. In fact the server only expects the connection of a client, the thread serves to not lock the graphical interface of the other program that is underneath.

  • From what I’m reading in the documentation, the class ServerSocket is closed after connections with it are closed. Is this not happening? Have you tried to see if you fall into the server’s catch block when you close?

  • It only casts the exception that is in the method run() if the customer is unable to connect(eg: port occupied). It never launches the method exception listener(), then it keeps the socket always open, even if the client disconnects

Show 2 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.