reading from socket

Hi!

I'd like to open a server socket on port 80, accept a clients call, read data from the client (usually a HTTP get/post request), print the data and close the socket.

Here is the code:

package test;

import java.io.IOException;

import java.io.InputStreamReader;

import java.net.ServerSocket;

import java.net.Socket;

publicclass Test{

publicstaticvoid main( String [] args )throws IOException{

int c;

ServerSocket serverSocket =new ServerSocket( 80 );

Socket clientSocket = serverSocket.accept();

InputStreamReader in =new InputStreamReader(clientSocket.getInputStream());

while( (c = in.read()) != -1 ) System.out.print( (char)c );

in.close();

clientSocket.close();

serverSocket.close();

}

}

I call this server from a client (browser form), and send a post request and both the server and the client block at thein.read() call, before the last line of the HTTP message. The server can read the first 6-7 lines of the message, but can not the last. What can cause this matter?

waczack

[1769 byte] By [waczacka] at [2007-10-3 4:27:14]
# 1

Your code will only complete if the client closes it's end of the socket, which it may well not do - especially if it uses the HTTP KeepAlive protocol.

You should probably consider wrapping the InputStreamReader (in) in a BufferedReader which will then allow you to read the HTTP headers line by line (using readLine()). This will also then allow you detect the end of the HTTP headers (a blank line):

BufferedReader br = new BufferedReader(in);

while(true) {

String s = br.readLine();

if (s == null /*EOF*/ || s.length() == 0 /* End of headers */) {

break;

}

System.out.println(s);

}

dannyyatesa at 2007-7-14 22:30:00 > top of Java-index,Core,Core APIs...
# 2
But I'd like to read the whole message, not just the header. The most importent for me is the posted data, which is in the message body.
waczacka at 2007-7-14 22:30:00 > top of Java-index,Core,Core APIs...
# 3

Sorry. Your message made it sound like you were only interested in the headers.

If this is what you want to do, and you believe that there will be content in the request - which is quite unusual - then you need to understand the HTTP protocol more fully. Specifically, you need to understand the Content-Length header, and possibly MIME encoding too.

There is nothing in the HTTP spec which says the client has to close its end of the socket when it has completed a request, so your code which relies on detecting EOF may never complete. As I already mentioned, this is especially true if the client is using HTTP KeepAlives - it will deliberately keep its socket open so it can make further requests without dropping and reconnecting the socket.

dannyyatesa at 2007-7-14 22:30:00 > top of Java-index,Core,Core APIs...
# 4

Yes, I thought about such solution, to use the content-length attribute. I only did not know, why my program stops before reading the last line. Now I realized, that I was tricked by NetBeans. It's output is not nesseccery flushed when you call system.out.flush(), and I got the last line every time.

Anyway thanks for the help!

waczacka at 2007-7-14 22:30:00 > top of Java-index,Core,Core APIs...