I'm trying to write a very simple java server which is mostly a copy-paste from Oracle tutorial and access it from browser by my LAN IP 192.168.0.106/?Q1.
My program outputs "Ready..." in terminal, but doesn't output the browser query Q1. Then I make second query Q2 to the same IP and program prints it: "GET /?Q2 HTTP/1.1...".
When I'm doing the same query to 127.0.0.1 (Q1 and Q2) surprisingly program outputs "Ready..." on first query, but print "null" on second.
Why am I loosing my first query? Is my in.readLine() causing it? And why when connecting to loopback 127.0.0.1 address I can get connection, but can't get my query printed?
int portNumber = 80;
System.out.println("Started...");
String s="#";
try (
ServerSocket serverSocket = new ServerSocket(portNumber);
Socket clientSocket = serverSocket.accept();
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
) {
System.out.println("Ready...");
s=in.readLine(); /// Let's take first line only
System.out.println(s);
} catch(Exception e){
System.out.println("Error "+e);
}
I'd like to see first query in output: "GET /?Q1...". Thank you.