2017-04-09 39 views
0

我一直在嘗試從服務器發送消息到我的應用程序。我正在使用應用程序「簡單套接字測試器」來創建服務器並嚮應用程序發送UTF-8消息。我從來沒有得到我的應用程序的消息,當我調試應用程序它卡在行「mServerMessage = mBufferIn.readLine();」永遠不會超越它。所以它似乎無法讀取消息。當我暫停調試器時,我來到「LocalSocketImpl.java」,它被卡在「private native FileDescriptor accept」上。我能做些什麼來防止這種情況發生?我的應用程序被困在mBufferIn.readLine()

我的TCP客戶端:

public TcpClient(OnMessageReceived listener) { 
    mMessageListener = listener; 

} 

/** 
* Sends the message entered by client to the server 
* 
* @param message text entered by client 
*/ 
public void sendMessage(String message) { 
    if (mBufferOut != null && !mBufferOut.checkError()) { 
     mBufferOut.println(message); 
     mBufferOut.flush(); 
    } 
} 


/** 
* Close the connection and release the members 
*/ 
public void stopClient() { 

    mRun = false; 

    if (mBufferOut != null) { 
     mBufferOut.flush(); 
     mBufferOut.close(); 
    } 

    mMessageListener = null; 
    mBufferIn = null; 
    mBufferOut = null; 
    mServerMessage = null; 
} 

public void run() { 

    mRun = true; 

    try { 
     //here you must put your computer's IP address. 
     InetAddress serverAddr = InetAddress.getByName(SERVER_IP); 

     Log.e("TCP Client", "C: Connecting..."); 

     //create a socket to make the connection with the server 
     Socket socket = new Socket(serverAddr, SERVER_PORT); 

     try { 

      //sends the message to the server 
      mBufferOut = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true); 
      sendMessage("hi"); 

      //receives the message which the server sends back 
      mBufferIn = new BufferedReader(new InputStreamReader(socket.getInputStream(),"UTF-8")); 



      //in this while the client listens for the messages sent by the server 
      while (mRun) { 

       mServerMessage = mBufferIn.readLine(); 
       System.out.println(mServerMessage); 

       if (mServerMessage != null && mMessageListener != null) { 
        //call the method messageReceived from MyActivity class 
        mMessageListener.messageReceived(mServerMessage); 
       } 

      } 

      Log.e("RESPONSE FROM SERVER", "S: Received Message: '" + mServerMessage + "'"); 

     } catch (Exception e) { 

      Log.e("TCP", "S: Error", e); 

     } finally { 
      //the socket must be closed. It is not possible to reconnect to this socket 
      // after it is closed, which means a new socket instance has to be created. 
     } 

    } catch (Exception e) { 

     Log.e("TCP", "C: Error", e); 

    } 

} 

//Declare the interface. The method messageReceived(String message) will must be implemented in the MyActivity 
//class at on asynckTask doInBackground 
public interface OnMessageReceived { 
    public void messageReceived(String message); 
} 

}

+0

你從服務器發送給客戶端的是什麼?服務器應該回復客戶端發送的「hi」嗎? – bmdelacruz

+0

不,「嗨」只是想知道我是否設法向服務器發送了一些東西。 – Kspr

+0

雖然我確實修復了它,但我看到了您的最後一條評論,而且實際上是問題所在。我添加了\ n現在它可以工作。所以謝謝! – Kspr

回答

1

您收到必須有 '\ n' 結尾處的字符串。 readLine方法將停止讀取或等待流,直到它讀取'\ n'。

+0

你知道是否有辦法讀取不以新行結束的數據?像另一個命令比readLine()? – Kspr

+0

我推薦你8號這個[答](http://stackoverflow.com/a/35446009/6396174)。 – bmdelacruz

+0

謝謝你的幫助,但我現在意識到我不認爲這會起作用。這是因爲服務器發送了我必須解析的JSON對象。我很抱歉打擾了你很多問題,但是你不知道該怎麼做? – Kspr

相關問題