Android客户端套接字IOException

时间:2017-06-08 18:07:28

标签: java android sockets client 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();
    }
}

}

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

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 个答案:

答案 0 :(得分:1)

调试已在上述评论中解决。

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

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