[问题]经济信息作业,教授给了一段java,但是每学过,求教!
发表于 : 2007-11-25 22:11
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,会发生什么?
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,会发生什么?