This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I have a TcpIpServer and a TcpIpClient written in Java.
I would like to communicate with my TcpIpServer without using my TcpIpClient.
To do this, I started the server, it listens on the port I specified but I get this reply when trying to visit it with a browser (http://localhost:xxxx):
No data received
Unable to load the webpage because the server sent no data.
Error code: ERR_EMPTY_RESPONSE
I understand this is happening because of the following lines of code:
The server received no input from the client's output stream and thus it closed the socket without doing anything. I assume this is the reason I get that error message.
I concluded I need to send the string GET_NEXT_NUMBER to the client's outputstream, so the server can process it and give me a proper reply.
But how do I do that just using the browser interface and without using any programming or scripting language? Is it possible? With some browser plugin maybe?
There is also something fundamental I do not understand and I would like an explanation of this please. I am trying to understand the TCP/IP conversation:
When I write a Servlet, the browser issues a GET request for me and doGet() gets called. I open a browser, I visit the Servlet URL and that GET request gets sent to the HTTP server, then to Tomcat so that the CGI program/the Servlet can handle my GET request using its doGet() method.
Now, the problem is that I do not understand how this conversation is happening with my simplistic TCP/IP server. It just listens to a port. There is no doGet(), there is only a GET.
I assumed that a GET request would happen automatically when I visited my server, but apparently it's not happening and I don't understand why. Using Chrome Developer tools, the only GET request I see upon visiting the server is this:
GET request for Chrome
I assume that GET request is simply to fetch Chrome's error page and has nothing to do with my server. So am I wrong? Does a GET request not happen automatically if you visit a server with a browser, connecting to x.x.x.x:xxxx? I know that in telnet you need to explicitly write a GET, but with a browser I would assume the GET happens automatically but it doesn't.. so how do I provoke a GET in this context then? I have no clue.
Thank you for reading.
//EDIT: I figured out that my original GET request was for a fact sent to the server, but the server did nothing with it because the client output stream (=server input stream) did not contain GET_NEXT_NUMBER. See here:
GET http://localhost:8765/GET_NEXT_NUMBER HTTP/1.1
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.69 Safari/537.36
I thought I had lost my GET request. So what I did is: I turned on recording on the Chrome Developer Tools and I managed to catch my original GET request.
The question still stands though. I do not know how to inject the string GET_NEXT_NUMBER into the client's output stream, just using a browser's address bar.
What am I doing with this request anyway? GET http://localhost:8765/GET_NEXT_NUMBER HTTP/1.1
Is that not saying the client's output stream starts with GET_NEXT_NUMBER? Apparently not, because otherwise the server would reply properly. I don't understand..
Unless the server implements HTTP (and it doesn't look like it does anything close to that), you can't use a browser to access it. You need a client that understands the same protocol the server implements.