for an mmo I am attempting to create, I have an android client connecting to a java server. The android is running on the emulator while the server is running straight on my computer. They can connect fine and acknowledge the connection but the client stops at an odd place and I can’t figure out why.
The server’s WorkerThread’s Run method:
public void run() {
try {
InputStream input = clientSocket.getInputStream();
OutputStream output = clientSocket.getOutputStream();
String returns="";
String s="";
try{
s= inputStreamToString(input).toString();
}
catch(Exception e)
{}
output.write(("HTTP/1.1 200 OK\n\nWorkerRunnable: " +
this.serverText + " - " +
"").getBytes());
output.close();
input.close();
} catch (IOException e) {
//report exception somewhere.
e.printStackTrace();
}
}
The InputStream stringbulider:
private static StringBuilder inputStreamToString(InputStream is) {
String line = "";
StringBuilder total = new StringBuilder();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
try {
while ((line = rd.readLine()) != null) {
total.append(line);
}
} catch (Exception e) {
}
return total;}
The client’s connection method:
public String sendMessage(String message)
{
try{ clientSocket = new Socket("10.0.2.2", 9000);
String modifiedSentence;
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
outToServer.writeBytes(message+"\n");
//Stops here
modifiedSentence = inFromServer.readLine();
outToServer.close();
inFromServer.close();
clientSocket.close();
Log.v(modifiedSentence, modifiedSentence);
return modifiedSentence;}
catch(Exception e)
{
return "";}
}
Thanks, any help will be appreciated.
EDIT: update if I close either the client or the server the other can recieve the former’s message but they can’t if both are open…
Try this:
Also check if the data is actually getting received by your server.