Take the 2-minute tour ×

I have a little problem with implementing socket connection between Android and IOS. While I'm using my application to connect two Android running devices everything works fine. But when I have to receive some data from Iphone app my readStream function is blocking and I can receive all data after the other part close the socket and it that way I can't return any response back to it. Here is what I am using for listening :

try {
    serverSocket = new ServerSocket(5000);
} catch (IOException e) {

    try {
            client = serverSocket.accept();

            // LOGS
            Log.w("READ","is connected : "+client.isConnected());
            Log.w("READ","port : "+client.getPort());
            Log.w("READ","ipadress : "+client.getInetAddress().toString());

            InputStream is = client.getInputStream();

            Log.w("READ","is Size : "+is.available());

            byte[] bytes = DNSUtils.readBytes(is);
            for(int i=0;i<bytes.length;i++){
                Log.w("READ","bytes["+i+"] : "+bytes[i]);

            try {
                Log.w("READ","packetType : "+bytes[4]);

                    DNSUtils.getPacketType(bytes[4], bytes, client);

            } catch(Exception e){
    } catch (IOException e) {
        ResponseERR pack = new ResponseERR();
        ResponseERR.errorMsg = "Socket TimeOut exception!";
        byte[] packet = pack.createNewPacket();

        try {
                OutputStream out = client.getOutputStream();
                Log.e("READDATAFROMSOCKET","ResponseERR Send.");
        } catch (Exception e1) {

and here is the function which I am using to convert InputStream to Byte Array :

public static byte[] readBytes(InputStream inputStream) throws IOException {
    // this dynamically extends to take the bytes you read
    ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();

    // this is storage overwritten on each iteration with bytes
    int bufferSize = 1024;
    byte[] buffer = new byte[bufferSize];

    // we need to know how may bytes were read to write them to the byteBuffer
    int len = 0;
    while ((len = inputStream.read(buffer)) != -1) {
        byteBuffer.write(buffer, 0, len);

    // and then we can return your byte array.
    return byteBuffer.toByteArray();

The both Iphone / Android app works like this:

  • First it creates Socket and send data to other device and close the OutputStream.
  • The second part is parsing the byte array and then return response using the same socket.

Any ideas how to change my functions so I can read the inputstream without blocking?


So the problem was on IOS side, it seems that Apple API require to close both streams of the created socket : read/write so the byte array can be send, which seems really stupid, because in that way the iphone app can't receive and parse my response.

share|improve this question
You can run the readStream method in a separate thread. –  assylias Aug 21 '12 at 11:02
I run this code in a service which is running on separate thread. –  Android-Developer Aug 21 '12 at 11:07

2 Answers 2

Contrary to the comment in your edit, it's your fault, not Apple's. You need to redesign your API. Your readBytes() method reads the stream until its end, which only happens when the peer closes the connection. If you just want to read some bytes, read(byte[]) already does that, and tells you how many, without requiring end of stream. I would just throw this method away.

share|improve this answer

With the experience of programming for RIM OS, I think all these IO operations should be performed in a different thread. The main UI thread would be asked to call another application thread using invokeLater() type methods where you check whether your asynchronous operation is complete and take appropriate action. That way, your code to read the input stream must be run in a separate thread.

share|improve this answer
I am creating the listening method in a service which is running on a separate thread, not in the main UI thread. –  Android-Developer Aug 21 '12 at 11:06
Then the one who is pumping data should flush it periodically for the other side to keep reading it. Otherwise, what is the issue if the reading thread is blocked on the stream (if the producer is slower than the consumer)? Is that freezing the device or something like that? –  Vikdor Aug 21 '12 at 11:08
The device don't freeze, the problem is that when Iphone app is sending me data I can read it in the while loop only if the socket it closed after it sends me the data. Otherwise I am receiving socket time out exception. I need to read the inputstream without blocking, because inputStream.read(buffer); is blocking and waiting until socket is closed, not really sure why. –  Android-Developer Aug 21 '12 at 11:12
Did you try flushing frequently on the client side (it is good to flush even for android client as well)? –  Vikdor Aug 21 '12 at 11:16
You mean flush the OutputStream when sending data? –  Android-Developer Aug 21 '12 at 11:19

Your Answer


