2016-10-04 77 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();