-1

私はWebクライアントを書いています。次のコードがあります。

public class Connection extends Thread{
public final static int PORT = 1337;
private ServerSocket svrSocket = null;
private Socket con  =  null;
public Connection(){

    try{
        svrSocket = new ServerSocket(PORT);
        System.out.println("Conected to: " + PORT);

    }catch(IOException ex)
    {
       System.err.println(ex);
       System.out.println("Unable to attach to port");
   }

}

public void run(){

while(true)
{

        try{
            con = svrSocket.accept();//on this part the program stops
            System.out.println("Client request accepted");
            PrintWriter out = new PrintWriter(con.getOutputStream());
            BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
            out.println("GET /<index.html> HTTP/1.1");

            out.println("***CLOSE***");
            System.out.println(in.readLine());
           /*
            String s;

            while((s = in.readLine()) != null)
            {
                System.out.println(s);
            }*/
            out.flush();
            out.close();
            in.close();
            con.close();

            System.out.println("all closed");
    }
        catch(IOException ex)
    {
        ex.printStackTrace();

    }



}
}

}

run メソッドは後で使用します。私が持っているのは、というファイルですindex.html。このファイルは、Java コードと同じファイルにあります。リクエストでやろうとしているのは、HTML ファイルを送信することです。しかし、このプログラムを Web ブラウザーで実行するとlocalhost:1337、次のように表示されます。

GET /<index.html> HTTP/1.1
***CLOSE***

これは表示されるべきではありません。の HTML コードの結果のページindex.htmlが表示されます。

Index.html コード:

<html>
 <head>
  <title>       </title>

 </head>
 <body bgcolor = "#ffffcc" text = "#000000">
  <h1>Hello</h1>
  <p>This is a simple web page</p>
 </body>
</html>

この HTML ページをブラウザに表示するにはどうすればよいですか?

ありがとうございました

4

2 に答える 2

0

あなたのコードはすべて問題ないようです。要求されたファイル名を取得し、ソケット出力ストリームを使用してファイルからの応答を書き込むことができるように、入力ストリームから HTTP ヘッダーを読み取る必要があるようです。

OutputStream output = con.getOutputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String fileName = readHeader(in);
String baseDir = System.getProperty("my.base.dir", "/home/myname/httpserver");
boolean exist = true;
InputStream fileIn = null;
try {
   File requestedFile = new File(baseDir, fileName);
   fileIn = new FileInputStream(requestedFile);
} catch(Exception e){
    exist = false;
}

String server = "Java Http Server";
String statusLine = null;
String typeLine = null;
String body = null;
String lengthLine = "error";

if (exist) {
   statusLine = "HTTP/1.0 200 OK" + "\r\n";
   //get content type by extension
   typeLine = "Content-type: html/text  \r\n";
   lengthLine = "Content-Length: " + (new Integer(fileIn.available())).toString() + "\r\n";
} else {
  statusLine = "HTTP/1.0 404 Not Found" + CRLF;
  typeLine = "text/html";
  body = "<HTML>" + "<HEAD><TITLE>404</TITLE></HEAD>" + "<BODY>404 Not Found"+"</BODY></HTML>";
}

output.write(statusLine.getBytes());
output.write(server.getBytes());
output.write(typeLine.getBytes());
output.write(lengthLine.getBytes());

output.write("\r\n".getBytes());

if (exist) {
   byte[] buffer = new byte[1024];
   int bytes = 0;

   while ((bytes = fileIn.read(buffer)) != -1) {
     output.write(buffer, 0, bytes);
   }
} else {
   output.write(body.getBytes());
}
//close sreams
于 2013-07-29T16:01:11.157 に答える