我有一個.Net c#TCPClient套接字與Android TCP服務器通信。我通過SSLStream從tcpclient發送一個Ascii編碼的字節數組到服務器,Android服務器正在響應並讀取數據流,但是在閱讀後它總是顯示特殊字符,但不是確切的數據。我也嘗試過用UTF-8和其他編碼以及SSLServersocket。當我從一天半的時間裏掙扎時,請幫助我。在此先感謝..Android TCP服務器流讀取
客戶端消息
byte[] sendingBlob = GenerateMessage(Encoding.ASCII.GetBytes(CLIENTSTATUS + "~" + AuthID + "~"), Encoding.ASCII.GetBytes("CheckForBusy"));
Generatemessage method will concatenate both length and data as single bytearray and returns it to sendingBLOB which is written to SSLStream
服務器插槽
public void OpenConnection() throws IOException {
//Create a server socket object and bind it to a port
ServerSocket socServer = new ServerSocket(SERVER_PORT);
//ServerSocket socServer=SSLServerSocketFactory.getDefault().createServerSocket(SERVER_PORT, 32, InetAddress.getLoopbackAddress());
//SSLServerSocket socServer= (SSLServerSocket) SSLServerSocketFactory.getDefault().createServerSocket(SERVER_PORT, 32, InetAddress.getLoopbackAddress());
//Create server side client socket reference
Socket socClient = null;
//Infinite loop will listen for client requests to connect
while (true) {
//Accept the client connection and hand over communication to server side client socket
socClient = socServer.accept();
//For each client new instance of AsyncTask will be created
ServerAsyncTask serverAsyncTask = new ServerAsyncTask();
//Start the AsyncTask execution
//Accepted client socket object will pass as the parameter
serverAsyncTask.execute(new Socket[] {socClient});
}
}
閱讀
InputStream inFromClient = mySocket.getInputStream();
byte[] data = new byte[4];
int count = inFromClient.read(data, 0,4);
String str = new String(data);
輸出
'我送一個ASCII編碼的字節array'。我不明白那是什麼。向我們展示您發送的內容。同時向我們展示您收到的內容。如果以十六進制表示不明確的後值。 – greenapps
我已經編輯了與Asciiencoded字節數組的問題以及輸出..你可以看看這個問題,並提供一個解決方案,因爲我致命地需要它.. –
你給unsifficirnt信息。我讓你告訴你發送的內容。我什麼也沒看見。我唯一看到的是你收到四個字節的值爲22,3,1,0。那麼,我可以檢查什麼?你甚至不知道這些值是否正確。奇怪的是你在讀取之前在緩衝區中顯示這些值。進一步的:如果一個4字節的長度被作爲消息的開始發送,那麼你確實發現了它自己的消息。如果這四個字節的確是這個長度,那麼你爲什麼要把它們串起來呢?然後將該字符串轉換回字節?請用十六進制符號表示更多的字節值。 – greenapps