ServerSocket接受抛出nullpointerexception

时间:2015-04-07 19:01:09

标签: android serversocket

我正在处理我正在处理的客户端服务器应用程序的问题,由于某种原因,当客户端尝试连接到它时,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)

0 个答案:

没有答案