分页: 1 / 1

[问题]经济信息作业,教授给了一段java,但是每学过,求教!

发表于 : 2007-11-25 22:11
diegowujun
import java.io.*;
import java.net.*;
public class TUC
{
public static void main(String[] s)
{
String firstLine;
Socket conn;
PrintStream out;
ServerSocket sock;
BufferedReader in;
try {
sock = new ServerSocket(80);
while (true)
{
// Wait for incoming tcp connection requests.
conn = sock.accept();
// Create input and output streams for the tcp connection.
in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
out = new PrintStream(conn.getOutputStream());
// Read the first line of the http request.
firstLine = in.readLine();
if (firstLine.indexOf(" /testseite ") != -1)
{
// Find the HTTP version number.
int verPos = firstLine.indexOf("HTTP");
// Print HTTP-Header
out.println("HTTP/1.1 200 OK");
out.println("Content-Type: text/plain");
// Print HTTP-Body
out.println();
out.println("Herzlich willkommen!");
out.print("Sie verwenden HTTP version ");
out.println(firstLine.substring(verPos+5, verPos+8 ) ) ;
}
else
{
out.println("HTTP/1.1 200 OK");
out.println("Content-Type: text/plain");
out.println();
out.println("Die angeforderte Seite ist nicht
verfügbar.");
}
// Close the connection.
conn.close();
}
} catch (IOException ioX){ System.out.println(ioX); };
}
}


这段程序看不懂,谁能帮我讲一下,谢谢拉,最好详细点!
附带作业问题?
1.假设:这个服务是您从本地开始的.当它得到一个要求(request)"testseite",会发生什么,例如:当你在浏览器内键入:http://localhost/testseite
2.这个sever的response是怎么样的?在你的浏览器中是什么导致的这个?
3.当键入http://localhost/eineandereseite,会发生什么?

发表于 : 2007-11-25 22:24
Amankwah
经济信息作业??做这个??

发表于 : 2007-11-25 22:30
diegowujun
是阿 没有办法,我们的教授经常出每教过的东西,我这个算简单的,还有被出过人家企业都每完成的项目!

发表于 : 2007-11-26 11:05
patrickhe
先把代码格式化一下

发表于 : 2007-11-26 11:52
yiller
这是一个模拟一个简单WEB服务器的JAVA代码
通过套接字SOCKET监听来自80端口的访问

1.假设:这个服务是您从本地开始的.当它得到一个要求(request)"testseite",会发生什么,例如:当你在浏览器内键入:http://localhost/testseite
2.这个sever的response是怎么样的?在你的浏览器中是什么导致的这个?
响应
HTTP/1.1 200 OK
Content-Type: text/plain

Herzlich willkommen!
Sie verwenden HTTP version
1.1

也就是在浏览器窗口中显示
Herzlich willkommen!Sie verwenden HTTP version 1.1

3.当键入http://localhost/eineandereseite,会发生什么?
响应
HTTP/1.1 200 OK
Content-Type: text/plain

Die angeforderte Seite ist nicht verfügbar.

也就是在浏览器窗口中显示
Die ....那一句

发表于 : 2007-11-26 17:52
diegowujun
非常感谢,真是太热心了.