hardartcore
hardartcore

Reputation: 17037

Android / Iphone socket communication

me and one of my coworkers are working on a simple socket communication between Android and Iphone application. The thing we achieved till now is I can send byte array to him in another thread and receive byte array in service. But the problem that we face is that I can't read the whole message which he is sending to if he don't close the socket. But I need that socket alive, so I can send him response if the data was ok or there were some problems. Here is how I am listening about his messages :

thread =  new Thread(new Runnable() {
        public void run() {
            try {
                serverSocket = new ServerSocket(5000);
                while(state){
                        client = serverSocket.accept();
                        client.setKeepAlive(true);

                        // LOGS
                        Log.d("","receivedBufferSize : "+serverSocket.getReceiveBufferSize());
                        Log.d("","is connected : "+client.isConnected());
                        Log.d("","port : "+client.getPort());
                        Log.d("","ipadress : "+client.getInetAddress().toString());

                        InputStream is = client.getInputStream();

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

                        byte[] bytes = toByteArray(is);
                        for(int i=0;i<bytes.length;i++){
                            Log.d("","bytes["+i+"] : "+bytes[i]);
                        }

                 }

                  serverSocket.close();
                  Log.d("","client socket : "+client.isClosed() + " serverSocket : "+serverSocket.isClosed());

             } catch (UnknownHostException e) {
                 e.printStackTrace();
             } catch (IOException e) {
                 e.printStackTrace();
             }
        }
    });
    thread.start();

So if he don't close the application or don't close the socket, I am sitting on that log message : Log.d("","is Size : "+is.available()); and nothing really happens. After he close the socket I can see the bytes of received byte array.

Any idea what I am doing wrong or any kind of suggestions / help what can cause this?

Thanks in advance!

Upvotes: 1

Views: 566

Answers (1)

hardartcore
hardartcore

Reputation: 17037

Actually the problem was on Iphone side, it was just a Input / Output Stream problems with Objective C implementation. So this piece of code is working properly.

Upvotes: 1

Related Questions