Need to have "stable" connection to server.
1. The client tries to connect to server every 5 (10, N)-seconds.
2. After sucess connection client receives data from server.
3. After interrupt connection (server turn off) - go to step  # 1.

How test:
1) Start server
2) Start client (to be sure that client get data from server)
3) Stop server
4) Wait some (about 200) attempts when client tries to connect to
server.
5) Start server.

Server sends data, but client doesn't get it.
socket.connect(...) is sucessfull, but
socket.getInputStream().read(byte[]) is wrong.
Thread blocks on input.read(..).
If uncomment line
//socket.setSoTimeout(500); //to set low-level timeout for reading
then input.read(..) throws timeout Exception.

Where is my wrong?
Thanks.

Part of client code:

    private void initSocket() {
        try {
            if (socket == null || socket.isClosed() == true
                    || socket.isConnected() == false) {
                socket = new Socket();
                // socket.setSoTimeout(500); // set low-level timeout
for read
                InetSocketAddress socketAddress = new
InetSocketAddress(
                        "192.168.1.3", 12344);
                notifyDataListener(4);
                socket.connect(socketAddress, 500);
                notifyDataListener(5);
            }
        } catch (Throwable t) {
            System.err.println(t);
        }
    }

    private void closeSocket() {
        try {
            if (socket != null && socket.isClosed() == false) {
                socket.close();
            }
        } catch (Throwable t) {
            System.err.println(t);
        }
    }

    private byte[] buffer = new byte[1024];

    public void run() {
        while (isActive) {
            try {
                notifyDataListener(1);
                initSocket();
                InputStream input = socket.getInputStream();
                int length = input.read(buffer);
                if (length < 0) {
                    throw new EOFException("Was got -1");
                }
                notifyDataListener(2);
            } catch (Throwable t) {
                closeSocket();
                notifyDataListener(3);
                try {
                    Thread.sleep(100);
                } catch (InterruptedException ie) {
                }
            }
        }
    }


Part of server code:

        while (true) {
            try {
                Socket s = ss.accept();
                InputStream input = s.getInputStream();
                final OutputStream output = s.getOutputStream();
                new Thread() {
                    public void run() {
                        while (true) {
                            try {
                                ik++;
                                String msg = "Hello # " + ik;
                                System.err.print("Send..." + msg);
                                output.write(msg.getBytes());
                                output.flush();
                                System.err.println(" OK!");
                            } catch (Throwable ioe) {
                                System.err.println(" WRONG!");
                                break;
                            }
                            try {
                                Thread.sleep(50);
                            } catch (Exception ioe) {
                            }
                        }
                    }
                }.start();

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to