简体   繁体   中英

Why can't my java server and android client send any message back and forth?

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:

outToServer.writeBytes(message+"\n");
outToServer.flush(); // Flush output to socket
modifiedSentence = inFromServer.readLine();

Also check if the data is actually getting received by your server.

A good practice will be to perform flush, after your perform the write.
This forces to write on the socket.
In addition, I would recommend you to place the close of the streams in the finally part
(I know it doesn't have to do with your question, but I would like to help you code better.
So a code that combines both my answers, should look like:

OutputStream outStream = .... //Get it somehow
try {
   outStream.write(data);
   outStream.flush();
   //Do other stuff if needed
} catch (Exception ex) {
 //Do something with the exception
}
finally {
   if (outStream != null) {
      try {
        outStream.close();
     } catch (IOException ioex) {
        //Ignore
     }
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM