views:

25

answers:

1

Hi,

I've just started looking at HTTP etc. and have written a simple Java Client that uses URLConnection to send a URL to a server and pulls down the index.html page (as plain text).

Now I'm working on a simple server but I'm stuck at the first hurdle, (well maybe 2nd or 3rd), I can't get it to respond to the client properly.

Here is the reading in loop and it reads in the HTTP request fine, even from FF and IE etc:

while((message = in.readLine()) != null)
    {
        System.out.println(message);
        out.write("something");
    }

The problem is that I don't know how to get it to respond anything useful. If I let it do what it is doing in the above code it sends "something" 6 times to my client (as there are 6 lines to the HTTP request) but nothing to FF/IE etc.

Also, it doesn't seem to break the loop ever as I added a System.out.println("test"); line to print after the loop but the server never seems to reach that point, should it? Should readLine() return null at the end of the first HTTP request?

I've been reading stuff on the sun and oracle websites but am still pretty stuck as to how this should work.

Thanks for your time,

Infinitifizz

EDIT: Oops, forgot to copy the code in.

Server.java:

package exercise2;

import java.net.*;

public class Server 
{
    public static void main(String[] args) throws Exception
    {
        boolean listening = true;
        ServerSocket server = new ServerSocket(8081);

    while(listening)
    {
        Socket client = server.accept();

        new ServerThread(client).start();
    }
        server.close();
    }
}

ServerThread.java:

package exercise2;

import java.io.*;
import java.net.*;

    public class ServerThread extends Thread 
{
    private Socket socket = null;
    public ServerThread(Socket s)
    {
        this.socket = s;
    }

    public void run()
    {
        try
        {

        PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
        BufferedReader in = new BufferedReader(new InputStreamReader(
                                    socket.getInputStream()));

        String message, reply = "";

        while((message = in.readLine()) != null)
        {
            System.out.println(message);
            out.write("something");
        }
            System.out.println("test");
        in.close();
        out.close();
        socket.close();
        }
        catch(IOException e)
        {
            System.err.println("error");
        }
    }
}
+1  A: 

Without seeing your client code, this is my best guess as to what's happening:

Your server is probably blocking in that readLine() because the client is done writing the request, but hasn't closed the connection (as it should: the client should wait around to get the response over that same connection). Typically, a HTTP server parses a request as it reads it: based on this, you can look for "\r\n\r\n" to demarcate the end of the header, and break out of your read loop at that point to parse the request and respond to it.

Seth
Also it's a good idea to close your streams in a finally block. In your ServerThread code sample you close the request/response streams inside a single try block, they should be moved to a finally block. This would ensure that if the call to in.readLine() or out.write() were to fail your underlying strams would still close and allow the connection to be released. This applies also for your client - and could well be the source of your problem too.
mmccomb
Thanks for the replys Seth and mmccomb but I'm still having the infinite loop problem. I've changed it so that the while loop now has: if(message.equals("\r\n\r\n")){break;} but it still doesn't break out of the loop. I'm also now using IE, FF and Chrome instead of my own client to minimise the chance for errors on my part. Any ideas what to do? should I be looking for "\r\n\r\n" or something else? Is my equals() syntax correct? Thanks
Infiniti Fizz
Okay I've now changed the if statement so it reads: if(message.endsWith("\r\n")){break;} but that still isn't working, am I just using the wrong combination of CRLFs? Where can I find how many should there be? Thanks again.
Infiniti Fizz
Each call to readLine() is probably only going to read one of those lines: each "\r\n" counts as a line (readLine() may even interpret this is two lines). Try either looking for your first blank line (i.e. message.length() == 0) or reading using something besides readLine(). (readLine() will probably also strip off any trailing "\n" characters).
Seth