Java Webserver没有响应

时间:2015-03-25 04:46:00

标签: java printwriter dataoutputstream

目标:

我使用此代码的目的是创建一个可以处理多个客户端的简单Web服务器,并且将使用html来响应" hi"当客户要求时。

代码:

这是第一名考试。它只能处理一个客户端一次:

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.*;
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());
             }
         }
     }
}

问题:

在我的计算机上,如果我运行第一个示例,并在我的浏览器上输入:&#34; http://192.168.1.xxx&#34;,我得到一个简单的&#34; hi&#34;。但是,在第二个问题上,如果我尝试同样的事情,它根本就不起作用。但是如果在命令提示符下输入:telnet 192.168.1.xxx 80,则键入GET它会发送回html。另外,如果我用PrintWriter替换DataOutputStream,它甚至不会将它发送到telnet。但是,我知道它会尝试,因为程序打印&#34; 0&#34;每次建立连接时,&#34; 1&#34;每次打印都有。

问题:

  • 阻止浏览器查看html的问题是什么?

  • 是否与html本身,设置连接的方式或DataOutputStream有关?

  • 我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

不要使用端口80,使用大于6000的其他随机端口。如果您没有正确关闭第一个程序,那么该程序仍然使用端口80。

我使用了与此类似的Http服务器程序。服务器还为每个连接创建多个线程,因此客户端数量不限于100。

`public class MultiThreadServer实现Runnable {      插座csocket;      static int portno;      static String结果;      MultiThreadServer(套接字csocket)      {       this.csocket = csocket;      }

 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);

            }
   }`