Path: csiph.com!usenet.pasdenom.info!gegeweb.org!eternal-september.org!feeder.eternal-september.org!mx04.eternal-september.org!.POSTED!not-for-mail From: Daniele Futtorovic Newsgroups: comp.lang.java.programmer Subject: Re: Java (android) socket reconnection Date: Sun, 09 Dec 2012 16:55:00 +0100 Organization: A noiseless patient Spider Lines: 98 Message-ID: References: <9a8716eb-5842-45d8-b62e-193122cd863e@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Injection-Date: Sun, 9 Dec 2012 15:55:19 +0000 (UTC) Injection-Info: mx04.eternal-september.org; posting-host="471490eb7dbb5112d18d2869517c5aa4"; logging-data="10760"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+ffa8OQtKPs92IQUcJ7YBH" User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:12.0) Gecko/20120428 Thunderbird/12.0.1 In-Reply-To: <9a8716eb-5842-45d8-b62e-193122cd863e@googlegroups.com> Cancel-Lock: sha1:eXi2h7XynPyep22c9i7ldS+s6c4= Xref: csiph.com comp.lang.java.programmer:20190 On 09/12/2012 16:06, artik allegedly wrote: > 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(); `sock.close()' instead of `sock = null' would be a start... although it's difficult to tell, as your code isn't complete. It seems to me you have another problem in thrd1, though. If sock.connect throws an Exception (for instance because the server isn't available) on the first run of thrd1, then sock will be non-null indefinitely, as far as I can see, even though it's not connected (thrd2 isn't started in that case). Looks like a design problem to me. BTW, don't you think you could do the whole routine in but one thread? The separation seems rather clunky. -- DF.