javaiosservergcdasyncsocket

How to read NSData into clientside made in java


I create an iOS server using GCDAsyncSocket and Client is created using Java language. When I sent data in form of NSDAta it can be read by SocketTester(application for testing client connection) but it cannot be completely read at java end .

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();
            }
        }
        */

}

I want to get data length sent through ios server.and create buffer accordingly.Can any one help me out?


Solution

  • I solved this using

    public static byte[] readChunkedData(final InputStream stream, final int size) throws IOException {
            byte[] buffer = new byte[size];
            DataInputStream din = new DataInputStream(stream);
            din.readFully(buffer);
            return buffer;
        }