Android client/Java server socket; Android sends but doesn’t receive?
I’ve been searching for four hours and it’s driving me crazy. If you need more info/code, I’ll try to keep it short and I’ll edit it.
So I have an Android client that uses PrintWriter and BufferedReader to connect to the server. The way it works is to start a new ASyncTask() to load the connection. Once the connection is established, it sends a “join” message to the server and then loads a listening thread that has a while loop that waits for UserInput.readLine() != null, and once interrupted, it returns a string of running processes to get the string and perform operations and reload the function of the listening task.
//Listener thread
class listen extends AsyncTask<Integer, Integer, Void> {
@Override
protected Void doInBackground(Integer... params) {
//Disconnect variable that's only turned true on backpress
if (!disconnect) {
try {
message = Connection.listen(); //socket object
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPostExecute(Void result) {
// async task finished
if (!disconnect) {
say("INCOMMING"); //easy-made function for Toast
input(message);
}
}
}
In that connection:
public String listen() throws IOException {
String userInput;
while ((userInput = in.readLine()) != null) {
}
return userInput;
}
Now in my server java application, I have a thread that loads other connection threads into the ArrayList and acts as a headquarters to send messages to all child clients
In my connection:
while ((inputLine = in.readLine()) != null) {
//Tells HQ to process string, with id being who it's coming from
hq.Process(id, inputLine);
if (!connected)
break;
}
In the HQ object:
public void Process(int id, String str) {
String[] msg = str.split(","); //split message
String send = " "; //string I print to console
if (msg[0].equals("join")) {
send = msg[1] + " has joined!";
parent.seats[cnew.get(id).seat] = id;
cnew.get(id).sendData();
System.out.println(id);
}
Once joined, HQ will tell that connection to send that player’s information to the phone
public void sendData() {
out.println("chips," + chips); // Update chip count
//give player his cards
out.println("card," + hq.parent.gameCards.getCard(10) + ","
+ hq.parent.gameCards.getCard(11));
//a cry for help to get some output on the phone
out.println("error,SAY THIS");
// out.flush(); //commented out because it didn't help at all
System.out.println("sending id " + id); //debug checker (ignore this)
}
My problem is that it works when I have four phones connected and they all send toasts to each other.
However, once I changed it to send back data as soon as the player joined, I didn’t get any response at all in Android.
I don’t understand why it doesn’t work. On the server side, it iterates through everything (checked via system.prints). The connection is established and when I click the button on the phone, the server is outputting its response. But the phone didn’t receive anything – I still don’t know if it was the server sending failure or the phone reading failure. Can you see anything in the code that might be causing this issue? Need to see more? Or are there any tips on how to debug the connection status? The listen() task will never finish its execution.
Update: So I found that this might be related to my while() loop on the android side, doh, which may never break. Stupid mistake. But I tried to add it as a tester and still got nothing:
public String listen() throws IOException {
String userInput;
while ((userInput = in.readLine()) != null) {
if (userInput.length() > 2)
break;
}
return userInput;
}
Update: The next desperate update –
When I hit “back” (which sends an exit message to the server that closes the connection, and calls out.close and rest.close), I get a never-ending “MSG” toast loop – the toast I put while typing is recognized. Does out.close cause blocking?
Best Solution
It turns out that the println on the server side doesn’t print a new line — add + “\n” to the end of the server message to get it through. Why? I do not know..