一个带Java / Socket的简单Http服务器?

我目前正在创建一个返回静态页面的小型HTTP服务器

Hello!

…我尝试使用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\r\n"); out.write("Date: Fri, 31 Dec 1999 23:59:59 GMT\r\n"); out.write("Server: Apache/0.8.4\r\n"); out.write("Content-Type: text/html\r\n"); out.write("Content-Length: 59\r\n"); out.write("Expires: Sat, 01 Jan 2000 00:59:59 GMT\r\n"); out.write("Last-modified: Fri, 09 Aug 1996 14:21:40 GMT\r\n"); out.write("\r\n"); out.write("Exemple"); out.write("

Ceci est une page d'exemple.

"); } // on ferme les flux. System.err.println("Connexion avec le client terminée"); out.close(); in.close(); clientSocket.close(); } }

此代码不包含任何错误,我从浏览器得到如下响应:

 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 

但我的问题是我在浏览器中没有页面? 有什么帮助吗?

PS:我已经读过这篇文章: http : //en.wikipedia.org/wiki/Hypertext_Transfer_Protocol ,(对不起法语…)

除了每个请求标题行后的\ r \ n,您还必须在标题后面发送一个空行。 例:

 out.write("HTTP/1.0 200 OK\r\n"); // Header... out.write("Last-modified: Fri, 09 Aug 1996 14:21:40 GMT\r\n"); out.write("\r\n"); // The content starts afters this empty line out.write("Hello!"); // Content... 

我更正了你的代码,以便它可以工作(但它仍然不完美,你应该在一个单独的线程中处理每个请求,例如使用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\r\n"); out.write("Date: Fri, 31 Dec 1999 23:59:59 GMT\r\n"); out.write("Server: Apache/0.8.4\r\n"); out.write("Content-Type: text/html\r\n"); out.write("Content-Length: 59\r\n"); out.write("Expires: Sat, 01 Jan 2000 00:59:59 GMT\r\n"); out.write("Last-modified: Fri, 09 Aug 1996 14:21:40 GMT\r\n"); out.write("\r\n"); out.write("Exemple"); out.write("

Ceci est une page d'exemple.

"); // on ferme les flux. System.err.println("Connexion avec le client terminée"); out.close(); in.close(); clientSocket.close(); } }

这只是对您上一个问题的回答以及浏览器中没有任何内容的原因是因为您错误地计算了字符数。

它应该是57而不是59。

更好的是自动计算字符数,但我相信你的样本只是一个样本。

你用的是什么机器? 什么OS? 如果您正在运行UNIX计算机,那么println将无法工作,因为它只发送一个LF字符。 HTTP需要CR和LF作为其标头。 尝试将\ r添加到字符串的末尾,看看是否有效。

哦,还有,你的:

  out.println("HTTP/1.0 200 OK"+ "Date: Fri, 31 Dec 1999 23:59:59 GMT"+ "Server: Apache/0.8.4"+ "Content-Type: text/html"+ "Content-Length: 59"+ "Expires: Sat, 01 Jan 2000 00:59:59 GMT"+ "Last-modified: Fri, 09 Aug 1996 14:21:40 GMT"+ 

它打印一根长串。

将这些更改为每个字符串的println,或将\ r \ n添加到字符串中。

每行输出之间需要正确的行分隔符( \r\n )。 仅仅连接它们是不够的 – 如果您打印出响应,就可以看到它们。

只是提醒一下:这不是 HTTP服务器,正如标题所暗示的那样。 它是一个套接字,它写出一个特定的硬编码HTTP响应(假设它是根据其他答案中的建议修复的)。 即使您动态更改了返回的内容和内容长度标头,仍然不足以符合HTTP协议。

当我在编写JLHTTP时学到了很多东西,HTTP还有很多东西。 并不是说它非常复杂,而是需要妥善处理许多其他细节和要求。 您可以阅读RFC(核心协议在RFC 7230中定义,或者更旧的RFC 2616)以了解有关此内容的更多信息。

我还可以提供JLHTTP源代码作为HTTP服务器的详细记录最小兼容实现的参考 – 它是一个文件,目前约3K行,其中几乎一半是文档。 它的目标是小巧但合规。 我认为查看代码对于想要了解HTTP服务器必须做什么的人来说非常有用。 正如我所说 – 不是很复杂,但有很多细节。

实际上,确切地说,JLHTTP并不是最小的 – 它确实包含一些有用的额外function,例如处理文件上传或支持HTTPS的多部分表单数据,HTTP协议本身不需要。 但是你可以轻松地将这些部分撕掉(或者只是跳过它们)并实现真正最小的Java HTTP服务器实现。