2013-10-07 19 views
0

我试图让此代码在单独的端口上运行5个单独的SocketServers。我从线程获取NullPointerExceptions,但我不明白为什么。一些帮助会很好。Java线程与ServerSockets的NullPointerException

线72是服务器的接受方法:

Socket套接字= server.accept();

public class TCPSocketServer { 

    /** 
    * Accept this many connections. 
    */ 
    private int my_backlog = 5; 

    /** 
    * The server socket. 
    */ 
    private ServerSocket server = null; 

    private static final int SERVICE_DISCOVERY_PORT = 1234; 
    private static final int ADDITION_SERVICE_PORT = 1235; 
    private static final int DIVISION_SERVICE_PORT = 1236; 
    private static final int MULTIPLICATION_SERVICE_PORT = 1237; 
    private static final int SUBTRACTION_SERVICE_PORT = 1238; 

    final static int[] SERVICE_PORTS = { 
           SERVICE_DISCOVERY_PORT, 
           ADDITION_SERVICE_PORT, 
           DIVISION_SERVICE_PORT, 
           MULTIPLICATION_SERVICE_PORT, 
           SUBTRACTION_SERVICE_PORT 
          }; 

    /** 
    * Create the server socket. 
    * @param a_port the port that the server socket should listen on. 
    */ 
    public TCPSocketServer(int port) { 

     ServerSocket server = null; 

      try { 
       server = new ServerSocket(port, my_backlog); 

       System.out.println("Service discovery listening on port " + port); 

      } catch (IOException ioe) { 
       ioe.printStackTrace(); 
      } catch (SecurityException se) { 
       se.printStackTrace(); 
      } 
    } // end TCPSocketServer method 


    public void listen() { 

     new Thread() { 
      public void run() { 
       while (true) {    
        try { 
         Socket socket = server.accept(); 

         DataInputStream in = new DataInputStream(socket.getInputStream()); 
         double dividend = in.readDouble(); 
         double divisor = in.readDouble(); 

         DataOutputStream out = new DataOutputStream(socket.getOutputStream()); 
         double result = 0; 
         result = dividend/divisor; // TODO: check for zero divisor 
         out.writeDouble(result); // TODO: separate into divide() method 
         out.flush(); 

         // tidy up 
         in.close(); 
         out.close(); 
         socket.close(); 

        } catch (IOException ioe) { 
         ioe.printStackTrace(); 
        } catch (SecurityException se) { 
         se.printStackTrace(); 
        } // end try/catch 
       } // end while loop 
      } // end run() 
     }.start(); // end Thread 
    } // end listen() 




// the main method 
    public static void main(String[] args) { 
     int port = 0; 

      for(int servicePort : SERVICE_PORTS){ 
       // Create the server 
       TCPSocketServer socketServer = new TCPSocketServer(servicePort); 
       // Listen on the server socket. This will run until the program is 
       // killed. 
       socketServer.listen(); 
      } 

    } // end main 
} 

下面是一些示例输出:

Service discovery listening on port 1234 
Exception in thread "Thread-0" java.lang.NullPointerException 
    at TCPSocketServer$1.run(TCPSocketServer.java:72) 
Service discovery listening on port 1235 
Exception in thread "Thread-1" java.lang.NullPointerException 
    at TCPSocketServer$1.run(TCPSocketServer.java:72) 
Service discovery listening on port 1236 
Exception in thread "Thread-2" Service discovery listening on port 1237java.lang.NullPointerException 
    at TCPSocketServer$1.run(TCPSocketServer.java:72) 

Exception in thread "Thread-3" Service discovery listening on port 1238 
java.lang.NullPointerException 
    at TCPSocketServer$1.run(TCPSocketServer.java:72) 
Exception in thread "Thread-4" java.lang.NullPointerException 
    at TCPSocketServer$1.run(TCPSocketServer.java:72) 
+2

请在您的服务器代码中确定第**行** **。 –

+0

我把它粘贴到一个编辑器中,通过我的推算,L72是匿名的'Thread'对象中的'ioe.printStackTrace()',它不能为空。 – Faelkle

回答

3

ServerSocket server = nullTCPSocketServer(int port)构造函数阴影的ServerSocket server会员上涨。从你的构造函数中取出一个,你应该很好走。

+1

然后用数据流修复重复的关闭问题。它会在这个问题得到解决之后抛出异常:)。 –

+1

你知道,自从我用Java编写套接字已经很长时间了,但我记得实际上是在一个线程中执行'accept',然后产生新的线程来处理返回的'Socket's。我怀疑这个实现实际上是单线程的,这可能不是作者想要的(但不是他们要求的;)*编辑*我把它取回来 - 它看起来像每个“SERVICE_PORT”的一个线程:) – Faelkle