How to shut down socket within an infinite loop?

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 shut down 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 port will remain open and listening. What should I do to resolve 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();
        }
    }
}

Thank you in advance !!

    
asked by anonymous 03.01.2018 / 18:30

0 answers