0

我试图让这段代码在不同的端口上运行 5 个独立的 SocketServer。我从线程中得到 NullPointerExceptions 但我不明白为什么。一些帮助会很棒。

第 72 行是服务器的接受方法:

套接字套接字 = 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)
4

1 回答 1

3

ServerSocket server = null在您的TCPSocketServer(int port)构造函数中将ServerSocket server成员隐藏在更高的位置。从你的构造函数中取出一个,你应该很高兴。

于 2013-10-07T22:50:14.223 回答