Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #20189

Java (android) socket reconnection

Newsgroups comp.lang.java.programmer
Date 2012-12-09 07:06 -0800
Message-ID <9a8716eb-5842-45d8-b62e-193122cd863e@googlegroups.com> (permalink)
Subject Java (android) socket reconnection
From artik <olsztyn.arti@gmail.com>

Show all headers | View raw


Hi,
Could somebody help me resolve I think small problem for You but huge for me.
How to correctly support reconnecting client sockets.
I have one thread (it's name thrd1) for controlling connection (and reconnection if is it needed) and thread (it's name thrd2) for cyclical sending data to server (it is written in Delhi).
Some strange happends when I stop server and start it again after some time.
Server receives information that my client (java-android) wants to connect several times (it depends on time - how long server doesn't respond) - but after these attempts connection back to almost normal state. 
Problem is in these too many attempts in connection and in this that when time not responding of server is enough long my application crushes.

In my opinion problem is in "not-cleaning" socket after my stop method? How to do it perfectly?

Could somebody help me to resolve my huge problem?
Regards
Artik

If I can I put my example code:
thrd2 = new Thread(new Runnable() {
public void run() {
    while (!Thread.interrupted()) {
        try {
            if (sock != null) {
                out.write("TEST DATA\n");
                out.flush();
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            } 
            //if sock is null wait 300ms
            else {
                try {
                    Thread.sleep(300);
                } catch (InterruptedException e) {
                     e.printStackTrace();
                }
            }
        } catch (IOException e) {
            try {
                sock=null;
                Thread.sleep(1000);
            } catch (InterruptedException e1) {
                 e.printStackTrace();
            }
        }
    }
}


and


thrd1 = new Thread(new Runnable() {
public void run() {
    while (!Thread.interrupted()) {
        try {
            Thread.sleep(100);
        } catch (InterruptedException e1) {

        }
        if (sock==null)
        try {
            sock = new Socket();
            sock.connect(new InetSocketAddress(
                    address, 5000), 300);
            r = new BufferedReader(new InputStreamReader(
                    sock.getInputStream()));
            out = new BufferedWriter(
                    new OutputStreamWriter(sock
                            .getOutputStream()));
            if ((thrd2!=null)&&(!thrd2.isAlive()))
            thrd2.start();
        } catch (UnknownHostException e) {
             e.printStackTrace();

        } catch (IOException e) {
             e.printStackTrace();
        }
    }
    ;
}
});
if ((thrd1!=null)&&(!thrd1.isAlive())) thrd1.start();

Back to comp.lang.java.programmer | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Java (android) socket reconnection artik <olsztyn.arti@gmail.com> - 2012-12-09 07:06 -0800
  Re: Java (android) socket reconnection Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2012-12-09 16:55 +0100
    Re: Java (android) socket reconnection artik <olsztyn.arti@gmail.com> - 2012-12-09 08:26 -0800
      Re: Java (android) socket reconnection Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2012-12-09 19:07 +0100
        Re: Java (android) socket reconnection artik <olsztyn.arti@gmail.com> - 2012-12-09 12:36 -0800
          Re: Java (android) socket reconnection Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2012-12-09 23:07 +0100
            Re: Java (android) socket reconnection artik <olsztyn.arti@gmail.com> - 2012-12-09 14:45 -0800
              Re: Java (android) socket reconnection Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2012-12-10 00:47 +0100
            Re: Java (android) socket reconnection artik <olsztyn.arti@gmail.com> - 2012-12-10 13:09 -0800
              Re: Java (android) socket reconnection Lew <lewbloch@gmail.com> - 2012-12-10 13:52 -0800
    Re: Java (android) socket reconnection artik <olsztyn.arti@gmail.com> - 2012-12-09 08:34 -0800
  Re: Java (android) socket reconnection Eric Sosman <esosman@comcast-dot-net.invalid> - 2012-12-09 11:09 -0500
    Re: Java (android) socket reconnection artik <olsztyn.arti@gmail.com> - 2012-12-09 08:33 -0800

csiph-web