我正在处理我正在处理的客户端服务器应用程序的问题,由于某种原因,当客户端尝试连接到它时,ServerSocket类接受消息正在抛出NullPointerException。
以下是负责服务器的类:
public class DeviceNameServer implements Runnable {
@Override
public void run() {
try {
ServerSocket serverSocket = new ServerSocket(Utils.SERVERPORTDEVICENAME);
while (!Thread.currentThread().isInterrupted()) {
Socket clientSocket = serverSocket.accept();
Thread t = new Thread(new DeviceNameRunner(clientSocket, new DefaultDeviceIdentifier()));
t.start();
}
} catch (IOException e) {
Log.e("CalpolException", e.getLocalizedMessage());
}
}
}
这是由活动
调用的nameServerThread = new Thread(new DeviceNameServer());
nameServerThread.start();
nameServerThread
是活动的成员
这是负责连接服务器的代码:
Socket socket = new Socket();
try {
socket.connect(new InetSocketAddress(serverIp, Utils.SERVERPORTDEVICENAME), 500);
InputStream is = socket.getInputStream();
while (socket.isConnected()) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder out = new StringBuilder();
socket.close();
break;
}
} catch (IOException e) {
e.printStackTrace();
}
一旦执行此serverSocket.accept()
消息,就会抛出NullPointerException
有没有人见过这个?
堆栈跟踪
java.lang.NullPointerException
at java.net.PlainSocketImpl.accept(PlainSocketImpl.java:102)
at java.net.ServerSocket.implAccept(ServerSocket.java:203)
at java.net.ServerSocket.accept(ServerSocket.java:128)
at za.co.strikemedia.calpol.babymonitor.DeviceNameServer.run(DeviceNameServer.java:17)
at java.lang.Thread.run(Thread.java:841)