TCP套接字连接无法在Android 4.4上运行

时间:2015-06-24 17:14:41

标签: android sockets tcp

由于某种原因,在Android 4.4上未建立通过Socket的TCP连接时遇到了一个奇怪的问题。它在4.1.1和5.0上运行良好。

    @Override
    protected Void doInBackground(Void... params) {
        try {
            // Creating InetAddress object from ipNumber passed via constructor from IpGetter class.
            InetAddress serverAddress = InetAddress.getByName(host); 

            // Create a new Socket instance and connect to host
            socket = new Socket(serverAddress, port);
            Log.i(TAG, "Socket created: " + host + ":" + port);

            // Create PrintWriter object for sending messages to server.
            out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true); //thread freezes here

            //Create BufferedReader object for receiving messages from server.
            in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

            Log.d(TAG, "In/Out created");

        } catch (UnknownHostException e) {
            Log.e(TAG, "Don't know about host: " + host + ":" + port);
            connected = false;
            Log.e(TAG, e.getMessage());
        } catch (IOException e) {
            Log.e(TAG, "Couldn't get I/O for the connection to: " + host + ":" + port);
            connected = false;
            Log.e(TAG, e.getMessage());
        }
        connected = true;
        return null;
    }

由于某种原因,线程在创建I / O时停止。

任何想法是什么原因造成的?也许之前有人见过这个问题?

0 个答案:

没有答案