一个带有 Java/Socket 的简单 Http 服务器? [英] A Simple Http Server with Java/Socket?
问题描述
我目前正在创建一个小型 HTTP 服务器,它返回一个静态页面 <p>Hello!</p>
...我尝试使用 Java 套接字:
I am currently creating a small HTTP server that returns a static page <p>Hello!</p>
...
I tried with sockets with Java:
public static void main(String[] args) throws Exception {
// création de la socket
int port = 1989;
ServerSocket serverSocket = new ServerSocket(port);
System.err.println("Serveur lancé sur le port : " + port);
// repeatedly wait for connections, and process
while (true) {
// on reste bloqué sur l'attente d'une demande client
Socket clientSocket = serverSocket.accept();
System.err.println("Nouveau client connecté");
// on ouvre un flux de converation
BufferedReader in = new BufferedReader(
new InputStreamReader(clientSocket.getInputStream())
);
PrintWriter out = new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(clientSocket.getOutputStream())),
true);
// chaque fois qu'une donnée est lue sur le réseau on la renvoi sur le flux d'écriture.
// la donnée lue est donc retournée exactement au même client.
String s;
while ((s = in.readLine()) != null) {
System.out.println(s);
out.write("HTTP/1.0 200 OK
");
out.write("Date: Fri, 31 Dec 1999 23:59:59 GMT
");
out.write("Server: Apache/0.8.4
");
out.write("Content-Type: text/html
");
out.write("Content-Length: 59
");
out.write("Expires: Sat, 01 Jan 2000 00:59:59 GMT
");
out.write("Last-modified: Fri, 09 Aug 1996 14:21:40 GMT
");
out.write("
");
out.write("<TITLE>Exemple</TITLE>");
out.write("<P>Ceci est une page d'exemple.</P>");
}
// on ferme les flux.
System.err.println("Connexion avec le client terminée");
out.close();
in.close();
clientSocket.close();
}
}
此代码不包含任何错误,我从浏览器收到如下响应:
this code does not contain any errors and I got a response from the browser like this:
GET / HTTP/1.1
Host: localhost:1989
Connection: keep-alive
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.46 Safari/536.5 Comodo_Dragon/19.0.3.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding: gzip,deflate,sdch
Accept-Language: fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
但我的问题是我在浏览器中没有页面?有什么帮助吗?
But my problem is that I get no page at the Browser? Any help please?
PS:我已经读过这篇文章:http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol ,(对不起法语...)
PS: I already read this article: http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol , (im sorry for the french language...)
推荐答案
除了在每个请求头行之后的
之外,你必须在头之后发送一个空行.示例:
In addition to the
after every request header line, you have to send an empty line after the header. Example:
out.write("HTTP/1.0 200 OK
");
// Header...
out.write("Last-modified: Fri, 09 Aug 1996 14:21:40 GMT
");
out.write("
"); // The content starts afters this empty line
out.write("<TITLE>Hello!</TITLE>");
// Content...
我更正了您的代码以使其正常工作(但它仍然不完美,您应该在单独的线程中处理每个请求,例如使用 java.util.concurrent.ThreadPoolExecutor
):
I corrected your code so that it works (but it is still not perfect, you should handle every request in a seperate thread, e.g. with java.util.concurrent.ThreadPoolExecutor
):
public static void main(String[] args) throws Exception {
// création de la socket
int port = 1989;
ServerSocket serverSocket = new ServerSocket(port);
System.err.println("Serveur lancé sur le port : " + port);
// repeatedly wait for connections, and process
while (true) {
// on reste bloqué sur l'attente d'une demande client
Socket clientSocket = serverSocket.accept();
System.err.println("Nouveau client connecté");
// on ouvre un flux de converation
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream()));
// chaque fois qu'une donnée est lue sur le réseau on la renvoi sur
// le flux d'écriture.
// la donnée lue est donc retournée exactement au même client.
String s;
while ((s = in.readLine()) != null) {
System.out.println(s);
if (s.isEmpty()) {
break;
}
}
out.write("HTTP/1.0 200 OK
");
out.write("Date: Fri, 31 Dec 1999 23:59:59 GMT
");
out.write("Server: Apache/0.8.4
");
out.write("Content-Type: text/html
");
out.write("Content-Length: 59
");
out.write("Expires: Sat, 01 Jan 2000 00:59:59 GMT
");
out.write("Last-modified: Fri, 09 Aug 1996 14:21:40 GMT
");
out.write("
");
out.write("<TITLE>Exemple</TITLE>");
out.write("<P>Ceci est une page d'exemple.</P>");
// on ferme les flux.
System.err.println("Connexion avec le client terminée");
out.close();
in.close();
clientSocket.close();
}
}
这篇关于一个带有 Java/Socket 的简单 Http 服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!