我有一个.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