[英]Android client-server app - readLine() does't work
I'm writing an a client(android)-server(java) app based on Socket. 我正在写一个基于Socket的客户端(android)-服务器(java)应用。 My problem is that I need to handle two types of message on server (MINDWAVE and SPHERO).
我的问题是我需要处理服务器上的两种类型的消息(MINDWAVE和SPHERO)。 The mindwave message is well processed by server but I've got a problem with the sphero one: -client sends the message "SPHERO" to sever.
服务器可以很好地处理mindwave消息,但是sphero一个存在问题:-client将消息“ SPHERO”发送给服务器。 -server prints "Sphero request catched."
-server打印“捕获了Sphero请求”。 and goes through the rest of its code fine -client stucks on its "while((fromServer = in.readLine())!=null)" loop (it doesn't even start the loop's first operation - just stucks on readline() part).
并仔细检查其余代码-客户端卡在“ while((fromServer = in.readLine())!= null)”循环中(它甚至没有启动循环的第一个操作-只是卡在readline()上部分)。
The client's thread 客户端线程
class SendSpheroRequest extends AsyncTask<Void, Void, Void> {
String fromServer = "";
int movement;
@Override
protected Void doInBackground(Void... params) {
try {
Thread.sleep(2000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
while (isActive) {
try {
socket = new Socket(address, port);
Thread.sleep(1000);
out = new PrintWriter(socket.getOutputStream(), true);
out.write(TAG);
out.flush();
out.close();
socket = new Socket(address, port);
in = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
while ((fromServer = in.readLine()) != null) {
Toast.makeText(getApplicationContext(), fromServer,
Toast.LENGTH_SHORT).show();
if (!fromServer.equalsIgnoreCase("")) {
try {
movement = Integer.parseInt(fromServer);
if (movement > 0) {
driveUp();
} else if (movement < 0) {
driveDown();
}
tvPosition.setText(movement + "");
} catch (Exception e) {
e.printStackTrace();
movement = 0;
}
fromServer = "";
}
}
Thread.sleep(1000);
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
And the server's message handling: 和服务器的消息处理:
public void processMessage(String message) {
Message messageObject = new Message(message);
if (messageObject.getClientType() == DEVICE_TYPE.MINDWAVE) {
System.out.println("Message sent by "
+ messageObject.getClientType() + " with ID="
+ messageObject.getClientID() + ". The attention value is "
+ messageObject.getAttention());
switch (messageObject.getClientID()) {
case 1: {
if (firstClientIterator < 5 && gameStarted
&& messageObject.getAttention() != 0) {
firstClientAttentionSum += messageObject.getAttention();
firstClientIterator++;
System.out.println("sum=" + firstClientAttentionSum
+ " iterator=" + firstClientIterator);
}
}
break;
case 2: {
if (secondClientIterator < 5 && gameStarted
&& messageObject.getAttention() != 0) {
secondClientAttentionSum += messageObject.getAttention();
secondClientIterator++;
System.out.println("sum=" + secondClientAttentionSum
+ " iterator=" + secondClientIterator);
}
}
break;
default:
System.err
.println("Cannot process the message. Hint: wrong id detected.");
}
} else if (messageObject.getClientType() == DEVICE_TYPE.SPHERO) {
System.out.println("Sphero request catched.");
try {
toClientPrintWriter = new PrintWriter(clientSocket.getOutputStream(), true);
if (firstClientIterator == 5 && secondClientIterator == 5) {
int difference = firstClientAttentionSum
- secondClientAttentionSum;
System.out.println("Sending data to Sphero. "
+ "The difference is " + difference + ".");
firstClientIterator = secondClientIterator = firstClientAttentionSum = secondClientAttentionSum = 0;
toClientPrintWriter.println(difference+"");
} else {
toClientPrintWriter.println("No results yet.");
}
toClientPrintWriter.flush();
toClientPrintWriter.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
readLine()
only returns when it reads a newline or when the stream closes. readLine()
仅在读取换行符或流关闭时返回。 So you should send "SPHERO\\n"
. 因此,您应该发送
"SPHERO\\n"
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.