Nagarz
Nagarz

Reputation: 166

Socket server stuck

I'm trying to make a server/client to send text from client to server then sending back an ok message or something similar back to the client, but for some error that I can't see, either the server gets stuck right before sending the ok back to the client, or the client does not receive the message (I think it's the first one though). Any help is appreciated.

This is the server code:

class ActiveServer extends Thread {

    InputStream inStream;
    OutputStream outStream;

    public ActiveServer(InputStream inStream, OutputStream outStream) {
        this.inStream = inStream;
        this.outStream = outStream;
    }

    @Override
    public void run() {
        boolean ret = false;
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(inStream));
                PrintWriter writer = new PrintWriter(outStream);) {
            String line = null;
            while((line = reader.readLine()) != null) {
                String[] str = line.split(";");
                line = null;
                switch (str[0]) {
                case "insert" : //ret = SQLOptions.insert(str[1], str[2]);
                    System.out.println(str[1]);
                    break;
                }
                writer.print(ret);
                writer.flush();
                // As far as i can see it gets stuck at the end of this while, but I don't know why.
            }

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

}

public class Server {

    private static final int PORT = 39165;

    public static void main(String[] args) {

        try (ServerSocket server = new ServerSocket(PORT);) {
            System.out.println("Servidor online");
            ExecutorService service = Executors.newFixedThreadPool(10);
            while (true) {
                Socket client = server.accept();
                InetAddress ip = client.getInetAddress();
                SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
                Date time = new Date();
                System.out.print(sdf.format(time));
                System.out.println(" " + ip + " connected");
                InputStream inStream = client.getInputStream();
                OutputStream outStream = client.getOutputStream();
                service.execute(new ActiveServer(inStream,outStream));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}

And here goes the client code:

public class Telnet {

    static Console console = System.console();

    public static void connect(String ip, String port) {
        try(Socket socket = new Socket(ip, Integer.parseInt(port));
                PrintWriter writer = new PrintWriter(socket.getOutputStream());
                BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));) {

            String msg = null;
            while(true) {
                msg = console.readLine();
                writer.println(msg);
                writer.flush();

                if (msg.equals(".quit")) {
                    System.out.println("Exiting...");
                    break;
                }

                String input = reader.readLine();
                System.out.println(input);
            }


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

    public static void main(String[] args) {

        if(args.length < 2) {
            err.println("Telnet <ip> <port>");
            return;
        }

        if (console == null) {
            err.println("A console is not available");
            return;
        }

        connect(args[0], args[1]);

    }
}

Upvotes: 0

Views: 479

Answers (1)

David Conrad
David Conrad

Reputation: 16359

On the server side, you write the response without a terminating newline:

            writer.print(ret);

But on the client side, you read until the end of line:

            String input = reader.readLine();

The documentation for BufferedReader#readLine says:

Reads a line of text. A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed.

Thus, the client will wait forever for the newline sequence which the server will never send.

Upvotes: 1

Related Questions