我编写了一个实现基本Web服务器的java类。它从控制台获取端口号。这段代码非常基础,它返回客户端发送的内容。这是代码:
package p106;
import java.io.*;
import java.net.*;
public class HttpMirror {
public static void main(String[] args) {
try
{
int port = Integer.parseInt(args[0]);
ServerSocket ss = new ServerSocket(port);
for(;;)
{
Socket client = ss.accept();
BufferedReader in = new BufferedReader( new InputStreamReader(client.getInputStream()));
PrintWriter out = new PrintWriter(client.getOutputStream());
out.print("HTTP/1.0 200 en");
out.print("Content-Type text/plain");
String line;
while( (line = in.readLine()) != null)
{
if(line.length() == 0)
break;
out.print(line );
}
out.close();
in.close();
client.close();
} // Tekrar döngüye gir ve bir sonraki bağlantıyı bekle.
}catch(Exception e)
{
System.err.println(e);
}
}
}
我运行此代码,我必须写什么到我的浏览器的地址栏?提前致谢。
你在这里尝试localhost:端口号吗?或者127.0.0.1:端口号在这里?
例如,如果端口号为8050,则地址为localhost:8050或127.0.0.1:8050
您没有指定用于启动Java引擎的命令。
int port = Integer.parseInt(args[0]);
表示端口是在命令行上传递的。因此,无论您通过哪个端口,都应该放入Web浏览器。
例如:
java HttpMirror 12345
http://localhost:12345
首先你需要检查端口,然后http://localhost:Portnumber例如http://localhost:8050