2016-10-04 53 views
0

我有一個.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); 

輸出

Image the showing output in androidTCPserver(debug mode)

+0

'我送一個ASCII編碼的字節array'。我不明白那是什麼。向我們展示您發送的內容。同時向我們展示您收到的內容。如果以十六進制表示不明確的後值。 – greenapps

+0

我已經編輯了與Asciiencoded字節數組的問題以及輸出..你可以看看這個問題,並提供一個解決方案,因爲我致命地需要它.. –

+0

你給unsifficirnt信息。我讓你告訴你發送的內容。我什麼也沒看見。我唯一看到的是你收到四個字節的值爲22,3,1,0。那麼,我可以檢查什麼?你甚至不知道這些值是否正確。奇怪的是你在讀取之前在緩衝區中顯示這些值。進一步的:如果一個4字節的長度被作爲消息的開始發送,那麼你確實發現了它自己的消息。如果這四個字節的確是這個長度,那麼你爲什麼要把它們串起來呢?然後將該字符串轉換回字節?請用十六進制符號表示更多的字節值。 – greenapps

回答

1

問題出在C#DataStream類使用套接字。嘗試用下面的代碼替換你的C#代碼。

SSLStream _clientSocket; 
    //DataStream class 
    public DataStream(SSLStream clientSocket) 
    { 
     _clientSocket = clientSocket; 
    } 
    public void Write(string message) 
    { 
     int toSendLen = Encoding.ASCII.GetByteCount(message); 
     byte[] toSendBytes = Encoding.ASCII.GetBytes(message); 
     byte[] toSendLenBytes = BitConverter.GetBytes(toSendLen); 
     _clientSocket.Write(toSendLenBytes); 
     _clientSocket.Write(toSendBytes); 
    } 
    public String ReadString() 
    { 
     byte[] rcvLenBytes = new byte[4]; 
     _clientSocket.Read(rcvLenBytes, 0 , 4); 
     int rcvLen = BitConverter.ToInt32(rcvLenBytes, 0); 
     byte[] rcvBytes = new byte[rcvLen]; 
     _clientSocket.Read(rcvBytes, 0, rcvLen); 
     var ascii = Encoding.ASCII.GetString(rcvBytes); 
     return ascii; 
    } 
+0

這看起來像一個私人對話。你如何知道服務器代碼? – greenapps

+0

c#流是正確的,因爲它與c#TCP服務器正常工作,你是正確的,我們發送的前4個字節作爲數據的長度和剩餘的bytearray作爲數據。當我嘗試在android中讀取SSL流時,只會發生此問題。 –

0

讀取包含填充有剩餘的數據的長度的第一4個字節的字節數組使用此代碼

 byte[] datalength = new byte[4]; 
     inputStream.read(datalength, 0, 4); 
     //bytebuffer to convert bits of length 
     ByteBuffer byteBuffer = ByteBuffer.wrap(datalength); 
     //if(use_little_endian) 
     byteBuffer.order(ByteOrder.LITTLE_ENDIAN); 

     int lengthCount = byteBuffer.getInt();