我使用GCDAsyncSocket創建iOS服務器,客戶端使用Java語言創建。當我以NSDAta的形式發送數據時,它可以被SocketTester(用於測試客戶端連接的應用程序)讀取,但它不能在java端完全讀取。如何讀NSData到客戶端在java中製作
static void recorderServerClient() throws IOException{
int port =9892;
clientSocket = new Socket("localhost", port);
System.out.print("Connected to localhost"+port);
while (true){
InputStream inputStream = clientSocket.getInputStream();
try {
Thread.sleep(3000);
}
catch (InterruptedException e) {
e.printStackTrace();
}/*fetch data with help of output stream and write it with help of PrintWriter */
PrintWriter printWriterObj = new PrintWriter(clientSocket.getOutputStream());
printWriterObj.println();
printWriterObj.flush();
/*created an array for now static and assign some memory */ byte[] buffer = new byte[1000000];
int read;
while((read = inputStream.read(buffer)) != -1) {
String output = new String(buffer, 0, read);
System.out.print(output);
System.out.flush();
}
}
*/
}
我想要得到的數據長度通過IOS發送server.and創建緩衝區accordingly.Can任何一個可以幫助我嗎?
你看到了什麼問題?看起來你缺少Socket.accept()調用? –
我無法通過java的readInt方法讀取數據長度。 – Ruby