Android 客户端套接字 IOException

Android Client Socket IOException

编辑:问题出在 Client.class 中的输入,因此当 Android 设备收到来自服务器的应答时。这些是导致崩溃的以下几行:

        InputStream input = client.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(input));

        String text;

        while ((text = reader.readLine()) != null) {
            Log.d("ClientLog", "Received from Server"+ text);
        }

这是我在 Android 中的 Client.class:

public class Client implements Runnable{

public Client() {

}

@Override
public void run() {
    try {
        Log.d("ClientLog", "Socket creation incoming");
        Socket client = new Socket("localhost", 5555);
        Log.d("ClientLog", "Client has been started");

        // Streams:
        OutputStream out = client.getOutputStream();
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));

        InputStream input = client.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(input));
        // -----------------------------------------------------------------------

        writer.write("Test");
        writer.newLine();
        writer.flush();

        String text;

        while ((text = reader.readLine()) != null) {
            Log.d("ClientLog", "Received from Server"+ text);
        }

        writer.close();
        reader.close();

    } catch (UnknownHostException e) {
        Log.d("ClientLog", "Error: Host not found");
        e.printStackTrace();
    } catch (IOException e) {
        Log.d("ClientLog", "Error: IOException");
        e.printStackTrace();
    }
}

}

如果需要我的 Server.class 在 Java:

public class Server {

public static void main(String[] args) {

    ExecutorService executor = Executors.newFixedThreadPool(30);

    ServerSocket server;
    try {
        server = new ServerSocket(5555);
        System.out.println("Server has been started on Port 5555");

        while(true) {
            try {
                Socket client = server.accept();

                //Thread t = new Thread(new Handler(client));
                //t.start();
                executor.execute(new Handler(client));

            } catch (IOException e) {
                System.out.println("Error IOExceotion");
                e.printStackTrace();
            }
        }

    } catch (IOException e1) {
        e1.printStackTrace();
    }

}

Server.class所需的Handler.class:

public class Handler implements Runnable{
private Socket client;

public Handler(Socket pClient) {
    client = pClient;
}

@Override
public void run() {
    try {
                // Streams:
                OutputStream out = client.getOutputStream();
                PrintWriter writer = new PrintWriter(out);

                InputStream input = client.getInputStream();
                BufferedReader reader = new BufferedReader(new InputStreamReader(input));
                // -----------------------------------------------------------------------

                String text = null;

                while ((text = reader.readLine()) != null) {
                    writer.write(text+ "\n");
                    writer.flush();
                    System.out.println("Recieved from Client: "+ text);
                }

                //Close Streams
                writer.close();
                reader.close();

                client.close();
    } catch (IOException e) {
        System.out.println("Error IOException");
        e.printStackTrace();
    }
}

}

调试在上面的评论中解决了。

第 1 步:向 android 清单文件添加权限,如下所述:Java socket IOException - permission denied

第 2 步:更改 while ((text = reader.readLine()) != null) 检查。我猜 text = reader.readLine() != null 检查有问题。