【发布时间】:2019-07-23 13:31:09
【问题描述】:
我正在为 Android 中的服务器/客户端通信设置客户端线程。我可以很好地建立连接。但是当我尝试使用下面的代码循环时,程序只会进入 while 循环两次。 (我也尝试了 while(true) 但没有任何反应......)。
我需要线程等待客户端对象将 sendPicture/sendLinks 等更新为 true 以发送示例行 pw.println("PICTURE")。
在服务器端 (PC) 它工作得很好(创建 1 个 ServerSocket 用于通信,其他用于 FileTransfers。)
非常感谢任何帮助。
已经试过了:while(true)
public ClientSocketThread(String ip, int port, MainActivity.myClientSocketThreadHandler h, String message, Client c) {
this.IP = ip;
this.message = message;
this.port = port;
this.handler = h;
this.client = c;
this.clientName = c.getClientName();
}
public void run() {
try {
clientSocket = new Socket(this.IP, port);
Scanner is = new Scanner(clientSocket.getInputStream());
pw = new PrintWriter(clientSocket.getOutputStream(), true);
String line = "";
pw.println(message);
while (!line.equals("OVER")) {
Log.i("ClientSocketThread", "------------------In true---------------------------------------------");
if (is.hasNextLine()) {
System.out.println("Has next line");
line = is.nextLine();
}else{
line = "";
}
System.out.println("______________________________Line: " + line);
/**************************
* Connexion établie
*/
if (line.equals("PERMITTED")) {
Message msg = handler.obtainMessage();
msg.what = 1;
Bundle bundle = new Bundle();
bundle.putString("CONNECTION_NAME", this.connectionName);
msg.setData(bundle);
handler.sendMessage(msg);
client.setConnectionAllowed(true);
/**************************
* Connexion refusé
*/
} else if (line.equals("REFUSED")) {
Message msg = handler.obtainMessage();
msg.what = 2;
Bundle bundle = new Bundle();
bundle.putString("CONNECTION_NAME", this.connectionName);
msg.setData(bundle);
handler.sendMessage(msg);
disconnect();
}
/**
* On veut envoyer une image
*/
if (line.startsWith("DSS-PICTURE:")) {
System.out.println("Je suis dans le DDS-PICTURE");
Integer futurServerPort = Integer.valueOf(line.split(":")[1]);
PictureClientSocketThread pictureThread = new PictureClientSocketThread(this.IP, futurServerPort, client.getMyPictureHandler(), "None", client);
pictureThread.run();
}
if (client.getSendLink() == true) {
pw.println("LINK");
System.out.println("_______________________________SEND: LINK");
client.setSendLink(false);
}
if (client.getSendPicture() == true) {
pw.println("PICTURE");
System.out.println("_______________________________SEND: PICTURE");
client.setSendPicture(false);
}
if (client.getSendFile() == true) {
pw.println("FILE");
System.out.println("_______________________________SEND: FILE");
client.setSendFile(false);
}
if (client.getSendVideo() == true) {
pw.println("VIDEO");
System.out.println("_______________________________SEND: VIDEO");
client.setSendVideo(false);
}
}// End while
} catch (
Exception e) {
e.printStackTrace();
}
}
实际输出到此为止:
I/---MainActivity: ---------------绑定完成
I/ClientSocketThread: ------------------在真实中----------- ----------------------
I/System.out:有下一行
______________________________行:允许
I/ClientSocketThread: ------------------在真实中----------- ----------------------
【问题讨论】:
标签: java android sockets while-loop