Frage

Ich habe einfach nur angefangen bei HTTP usw. und haben ein einfaches Java-Client geschrieben, die URLConnection verwendet eine URL zu einem Server und nach unten zieht die index.html-Seite (als Klartext) zu senden.

Jetzt arbeite ich auf einem einfachen Server, aber ich bin an der ersten Hürde stecken, (na ja vielleicht 2. oder 3.), kann ich es nicht zu reagieren auf den Client erhalte richtig.

Hier ist das Lesen in der Schleife und liest sich in der HTTP-Anforderung in Ordnung, sogar von FF und IE etc:

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

Das Problem ist, dass ich weiß nicht, wie es zu bekommen, etwas Sinnvolles zu reagieren. Wenn ich tun lassen, was es in dem oben genannten Code tut es „etwas“ 6 mal zu meinem Client sendet (wie es 6 Zeilen auf die HTTP-Anforderung ist), aber nichts zu FF / IE etc.

Auch scheint es nicht die Schleife jemals zu brechen, wie ich eine System.out.println("test"); Linie nach der Schleife drucken hinzugefügt, aber der Server scheint nie diesen Punkt zu erreichen, soll es? Should readline () return null am Ende der ersten HTTP-Anforderung?

Ich habe Sachen auf der Sonne und Orakel-Websites zu lesen, aber bin immer noch ziemlich fest, wie diese funktionieren soll.

Vielen Dank für Ihre Zeit,

Infinitifizz

EDIT: Oops, vergaß in den Code zu kopieren

.

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");
        }
    }
}
War es hilfreich?

Lösung

First of all, change the condition in the while loop to
while(in.hasNextLine()) {
    message = in.nextLine();
    //etc....

Secondly, you don't need to exit out of the while loop while running a server. You should do all the parsing for requests inside the while loop, using if statements to distinguish the requests. The only time you would ever exit the while loop is when the connection should close, otherwise, the nextLine() method will block until something is received.

Andere Tipps

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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top