このコードを取得して、別々のポートで 5 つの別々の SocketServer を実行しようとしています。スレッドから NullPointerExceptions が発生していますが、その理由がわかりません。いくつかの助けは素晴らしいでしょう。
72行目はサーバーのacceptメソッドです:
ソケット 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)