2013-08-03 93 views
-1

我正在制作一个简单的Web服务器,每次客户端连接时都会产生一个新线程。然后我需要使用BufferedOutputStream发送响应(因为HTTP协议在行尾需要一个/ r/n)。 这里是我的代码:使用传递给线程的套接字时,“套接字已关闭”异常?

class HttpServer{ 
    public static void main(String args[]){ 
     ServerSocket ss = null; 
     int port = 40000; 

     try{ 
      if(args.length > 0){ 
       port = Integer.parseInt(args[0]); 
      } 

      ss = new ServerSocket(port); 

      while(true){ 
       Socket client = ss.accept(); 
       User user = new User(client); 
       user.start(); 
      } 
     } 
     catch(Exception e){ 
       System.err.println(e.getMessage()); 
     } 
    } 
} 

我的用户等级:

class User extends Thread{ 

     Socket client; 

     public User(Socket s){ 
      client = s; 
     } 

     public void run(){ 
      try{ 
       BufferedOutputStream bos = new BufferedOutputStream(client.getOutputStream()); 

       println(bos, "Hello World"); 
       bos.close(); 

       client.close(); 
      } 
      catch(Exception e){ 
        System.err.println(e.getMessage()); 
      } 
     } 

     private void println(BufferedOutputStream bos, String s) throws IOException { 
      String news = s + "\r\n"; 
      byte[] array = news.getBytes(); 
      for(int i=0; i<array.length; i++){ 
       bos.write(array[i]); 
      } 
      return; 
     } 
    } 

当我尝试创建的BufferedOutputStream出现的问题;它给了我一个“套接字已关闭”的错误,我不知道为什么。

任何帮助将不胜感激。

谢谢!

这是不起作用的代码:

class User extends Thread{ 

    Socket client; 

    public User(Socket s){ 
     client = s; 
    } 

    public void run(){ 
     try{ 
      System.out.println("Address: " + client.getInetAddress().toString()); 

      BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream())); 

      String line; 
      Boolean first = true; 
      while(!((line = reader.readLine()).equals(""))){ 
       if(first) 
        System.out.println(line.split(" ")[1]); 
       first = false; 
      } 
      reader.close(); 

      BufferedOutputStream bos = new BufferedOutputStream(client.getOutputStream()); 
      println(bos, "Hello World"); 
      bos.close(); 



      // Close socket 
      client.close(); 
     } 
     catch(Exception e){ 
       System.err.println(e.getMessage()); 
     } 
    } 

    private void println(BufferedOutputStream bos, String s) throws IOException { 
     String news = s + "\r\n"; 
     byte[] array = news.getBytes(); 
     for(int i=0; i<array.length; i++){ 
      bos.write(array[i]); 
     } 
     return; 
    } 
} 
+1

很奇怪,我不知道发生了什么 – tbodt

+0

客户端如何调用您的服务器?也许客户在从服务器读取任何输出之前关闭连接? – tangens

+0

我一直在浏览器中输入localhost:40000进行测试,这就是结果。我也尝试使用本地地址,例如192.168.0.100:40000具有相同的结果。 – Neurion

回答

0

到目前为止,我所知道的,有你的代码没有问题。操作系统或浏览器似乎更有可能以某种方式限制对服务器的访问。

-1
Socket client = ss.accept(); 
User user = new User(client); 
user.start(); 

accept呼叫等待连接的块。您的用户线程永远不会被创建。您应该创建一个新的User类,它为您创建一个新的线程并将您的消息写入服务器。

public class User extends Thread{ 

      Socket client; 
      int port; 
      public User(int p) throws UnknownHostException, IOException{ 
       port = p; 
       client = new Socket(InetAddress.getLocalHost(), 40000); 
      } 

      public void run(){ 
       try{ 
        BufferedOutputStream bos = new BufferedOutputStream(client.getOutputStream()); 

        println(bos, "Hello World"); 
        bos.close(); 

        client.close(); 
       } 
       catch(Exception e){ 
         System.err.println(e.getMessage()); 
       } 
      } 

      private void println(BufferedOutputStream bos, String s) throws IOException { 
       String news = s + "\r\n"; 
       byte[] array = news.getBytes(); 
       for(int i=0; i<array.length; i++){ 
        bos.write(array[i]); 
       } 

       return; 
      } 

      public static void main(String[] args) throws UnknownHostException, IOException { 
       User user = new User(4000); 
       user.start(); 
      } 
     } 

服务器应该只能听传入连接。(也许打印)

import java.net.ServerSocket; 
import java.net.Socket; 
import java.util.Scanner; 


class HttpServer{ 
    public static void main(String args[]){ 
     ServerSocket ss = null; 
     int port = 40000; 

     try{ 
      if(args.length > 0){ 
       port = Integer.parseInt(args[0]); 
      } 

      ss = new ServerSocket(port); 

      while(true){ 
       Socket client = ss.accept(); 
       Scanner scanner = new Scanner(client.getInputStream()); 
       while (scanner.hasNext()) { 
        System.out.println(scanner.nextLine()); 
       } 
      } 
     } 
     catch(Exception e){ 
       System.err.println(e.getMessage()); 
     } 
    } 
} 

运行两者不同的Java程序,你应该在服务器端看到的hello world。 如果你改变你的端口,你可能不得不在客户端改变它。

+0

User *是*接受的连接,而不是服务器内部的新客户端连接。你根本没有理解代码。 -1 – EJP

2

这个意外意味着关闭了Socket。

关闭输入流或套接字的输出流会关闭另外两个关闭任何FilterInput/OutputStream/Reader/Writer关闭它正在过滤的流/读取器/写入器。