Strange behavior in simple chat using Java

I started learning java last month, and now I'm trying to write a simple chat program, but I came across something strange and I was curious why this happened.

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

public class Server {
public static void main(String[] args) throws IOException {
    String text = "";
    ServerSocket ss = new ServerSocket(21025);
    while (true){

        System.out.println("Waiting...");
        Socket s1 = ss.accept();
        System.out.println("Connection accepted from "+s1.getInetAddress());
        PrintStream pout = new PrintStream(s1.getOutputStream());
        pout.println("Connected to the server");

        new Thread(new Ricevitore(s1)).start();
     }
  }
}

public class Ricevitore implements Runnable {
String text = "";
Socket skt;
public Ricevitore(Socket skt){
    this.skt = skt;
}
@Override
public void run() {
    while (!text.equalsIgnoreCase("end")) {
        try {
            InputStream in = skt.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            text = br.readLine();
            if (!text.equalsIgnoreCase("end"))
            System.out.println(text);
        }
        catch (IOException e){}
    }
  }
}

public class Client {
public static void main(String[] args) throws IOException {

    //Create a socket
    try (Socket s = new Socket("127.0.0.1", 21025)) {
        String text="";
        while(!text.equalsIgnoreCase("end")) {

           //Allows messages from server
            InputStream in = s.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            if (br.ready()) {
                Scanner server = new Scanner(br);
                String testoServer = br.readLine();
                System.out.println(testoServer);
            }

            //Allows to send text to the server
            OutputStream out = s.getOutputStream();
            PrintStream pout = new PrintStream(out);

            Scanner tastiera = new Scanner(System.in);
            text = tastiera.nextLine();
            pout.println(text);

        }
    }
  }
}

This is a complete program at the moment, my question is this: since I wanted not to print the word "end" in order to close the program, I inserted

 if (!text.equalsIgnoreCase("end"))

but after that the server does not display the message “connected to the server” unless I first entered something through the client. If I comment on this if statement, both the “Connection accepted” and “Connected to server” messages were printed at the same time as the destination. I do not know if my question is clear, and I am interested to know why this is happening.

, , .

+4
1

, .

, br.ready() true. , false, .

, , .

, if (!text.equalsIgnoreCase("end")) . , .

, . , , .

, , if .


: /, Scanner , .

, PrintStream , end . null br.readLine(). br run().

+1

Source: https://habr.com/ru/post/1656564/


All Articles