简体   繁体   English

为什么我的java服务器和android客户端不能来回发送任何消息?

[英]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. 对于mmo我正在尝试创建,我有一个连接到Java服务器的android客户端。 The android is running on the emulator while the server is running straight on my computer. 当服务器在我的计算机上直接运行时,android正在模拟器上运行。 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: 服务器的WorkerThread的Run方法:

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: 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. 在执行写入之后,一个好的做法是执行flush。
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
     }
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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