I’m facing small difficulties with my Java server, basically it works, but only for one line, when I’m adding another it doesn’t send it, here is the snippet for server:
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(), true);
if (toSend.length() != 0) {
out.print(toSend);
out.flush();
System.out.println("connected");
toSend.setLength(0);
changeStatusTS(NULL, true);
}
if (in.ready()) {
s = in.readLine();
if ((s != null) && (s.length() != 0)) {
appendToChatBox("INCOMING: " + s + "\n");
if (s.equals("51789181 19426953") == true) {
out.println(ms.lister().toString().replace('[', ' ').replace(']', ' ').trim().replace(',', ' ') + "" + '\n');
out.println(ms.topicDesc().toString().replace('[', ' ').replace(']', ' ').trim().replace(',', ' ') + "" + '\n');
out.flush();
}
}
}
and the receiving part:
clientSocket = new Socket("10.0.2.2", 1234);
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
sentenceX = "" + point.getLatitudeE6();
sentenceY = "" + point.getLongitudeE6();
outToServer.writeBytes(sentenceX + " " + sentenceY + '\n');
String interesting = inFromServer.readLine();
String[] holder = interesting.split("\\s+");
String title = inFromServer.readLine();
String[] titleHolder = title.split("\\s+");
dalvik monitor is printing the error as:
TCP Error: java.lang.ArrayIndexOutOfBoundsException
The program is working, but it doesn’t receive the second out.println();
just a guess: the second
inFromServer.readLine()call could be too fast. The second packet might not be sent/received at this time… maybe checking if the string isready()might be a good idea