gpt4 book ai didi

java - Android 客户端套接字 IOException

转载 作者:行者123 更新时间:2023-12-02 12:55:20 25 4
gpt4 key购买 nike

编辑:问题在于 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 步:向 Android list 文件添加权限,如下所述:Java socket IOException - permission denied

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

关于java - Android 客户端套接字 IOException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44442754/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com