Java Web服务器没有响应
目标: 我使用这段代码的目标是创建一个简单的web服务器,它可以处理多个客户机,当客户机请求它时,它会用html响应说“嗨” 代码: 这是第一个测试。它只能处理一个客户端一次:Java Web服务器没有响应,java,printwriter,dataoutputstream,Java,Printwriter,Dataoutputstream,目标: 我使用这段代码的目标是创建一个简单的web服务器,它可以处理多个客户机,当客户机请求它时,它会用html响应说“嗨” 代码: 这是第一个测试。它只能处理一个客户端一次: import java.net.*; import java.io.*; public class Webserver1 { public static void main(String[] args) { ServerSocket ss; Socket s;
import java.net.*;
import java.io.*;
public class Webserver1 {
public static void main(String[] args) {
ServerSocket ss;
Socket s;
try {
//set up connection
ss = new ServerSocket(80);
s = ss.accept();
} catch (Exception e) {
System.out.println(e.getMessage());
return;
}
try (
BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
DataOutputStream out = new DataOutputStream (s.getOutputStream());
) {
String inline = in.readLine();
//http request
if (inline.startsWith("GET")) {
//return http
out.writeBytes("<!doctype html><html><body><p>hi</p></body></html>");
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
import java.net.*;
导入java.io.*;
公共类Webserver1{
公共静态void main(字符串[]args){
服务器插座;
插座;
试一试{
//建立连接
ss=新服务器插座(80);
s=ss.accept();
}捕获(例外e){
System.out.println(e.getMessage());
返回;
}
试一试(
BufferedReader in=新的BufferedReader(新的InputStreamReader(s.getInputStream());
DataOutputStream out=新的DataOutputStream(s.getOutputStream());
) {
字符串inline=in.readLine();
//http请求
if(inline.startsWith(“GET”)){
//返回http
out.writeBytes(“hi”);
}
}捕获(例外e){
System.out.println(e.getMessage());
}
}
}
这是第二个测试。它用于处理多个客户端:
import java.net.*;
import java.io.*;
public class Webserver2 {
//class to handle connections
public static class server {
ServerSocket ss;
Socket s[] = new Socket[maxn];
public server () {
try {
ss = new ServerSocket(80);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
public InputStream getis(int num) throws Exception {
return s[num].getInputStream();
}
public OutputStream getos(int num) throws Exception {
return s[num].getOutputStream();
}
public void close() throws Exception {
for (int i = 0; i < numc; i++) {
s[i].close();
}
}
public void newc () throws Exception {
s[numc + 1] = ss.accept();
}
}
static int numc = 0;
static final int maxn = 100;
static server se = new server();
public static void main(String[] args) {
try {
while (numc < 6) {
//set up connection, and start new thread
se.newc();
numc++;
System.out.println("0");
(new Client()).start();
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
public static class Client extends Thread {
public void run() {
try(
BufferedReader in = new BufferedReader(new InputStreamReader(se.getis(numc)));
DataOutputStream out = new DataOutputStream (se.getos(numc));
) {
String inline;
while(true) {
inline = in.readLine();
//wait for http request
if (inline.startsWith("GET")) {
System.out.println("1");
//respond with header, and html
out.writeBytes("HTTP/1.1 200 OK\r\n");
out.writeBytes("Connection: close\r\n");
out.writeBytes("Content-Type: text/html\r\n\r\n");
out.writeBytes("<!doctype html><html><body><p>hi</p></body></html>");
out.flush();
}
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
}
import java.net.*;
导入java.io.*;
公共类Webserver2{
//类来处理连接
公共静态类服务器{
服务器插座;
套接字s[]=新套接字[maxn];
公共服务器(){
试一试{
ss=新服务器插座(80);
}捕获(例外e){
System.out.println(e.getMessage());
}
}
公共InputStream getis(int num)引发异常{
返回s[num].getInputStream();
}
public OutputStream getos(int num)引发异常{
返回s[num].getOutputStream();
}
public void close()引发异常{
对于(int i=0;i”);
out.flush();
}
}
}捕获(例外e){
System.out.println(e.getMessage());
}
}
}
}
问题:
在我的计算机上,如果我运行第一个示例,并在浏览器上键入:“”,我会得到一个简单的“hi”。然而,在第二种情况下,如果我尝试同样的事情,它根本不起作用。但是如果在命令提示符中输入:telnet 192.168.1.xxx 80,那么输入GET将返回html。此外,如果我用PrintWriter替换DataOutputStream,它甚至不会将其发送到telnet。但是,我知道它会尝试,因为程序每次建立连接时都会打印“0”,每次打印内容时都会打印“1”
问题:
- 阻止浏览器查看html的问题是什么
- 它是否与html本身、设置连接的方式或DataOutputStream有关
- 我怎样才能解决这个问题
publicstaticvoidmain(字符串参数[])
抛出异常
{
portno=Integer.parseInt(args[0]);
ServerSocket srvsock=新的ServerSocket(端口号);
System.out.println(“侦听”);
while(true){
Socket sock=srvsock.accept();
System.out.println(“已连接”);
新线程(新的多线程服务器(sock)).start();
}
}
公开募捐
{
字符串[]输入=新字符串[3];
FileInputStream fis=null;
字符串file=null,status=null,temp=null;
尝试
{
InputStreamReader ir=新的InputStreamReader(csocket.getInputStream());
BufferedReader br=新的BufferedReader(ir);
DataOutputStream out=新的DataOutputStream(csocket.getOutputStream());
字符串消息=br.readLine();
输入=message.split(“”);
if(输入[0]。等于(“获取”))
{
试一试{
出来
public static void main(String args[])
throws Exception
{
portno=Integer.parseInt(args[0]);
ServerSocket srvsock = new ServerSocket(portno);
System.out.println("Listening");
while (true) {
Socket sock = srvsock.accept();
System.out.println("Connected");
new Thread(new MultiThreadServer(sock)).start();
}
}
public void run()
{
String[] inputs=new String[3];
FileInputStream fis=null;
String file=null,status=null,temp=null;
try
{
InputStreamReader ir=new InputStreamReader(csocket.getInputStream());
BufferedReader br= new BufferedReader(ir);
DataOutputStream out = new DataOutputStream(csocket.getOutputStream());
String message=br.readLine();
inputs=message.split(" ");
if(inputs[0].equals("GET"))
{
try{
out.writeBytes("HTTP/1.1 200 OK\r\n");
out.writeBytes("Connection: close\r\n");
out.writeBytes("Content-Type: text/html\r\n\r\n");
out.writeBytes("<!doctype html><html><body><p>hi</p></body> </html>");
}
out.flush();
fis.close();
csocket.close();
}
catch(Exception ex)
{
status="404 File not found";
os.println(status);
}
}`