Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!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 23:07:13 +0100 Organization: A noiseless patient Spider Lines: 48 Message-ID: References: <9a8716eb-5842-45d8-b62e-193122cd863e@googlegroups.com> <27f309f0-5118-4fa8-8b26-858d19af27de@googlegroups.com> <37e29245-591a-4325-b767-fdfe8d09a703@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Injection-Date: Sun, 9 Dec 2012 22:07:38 +0000 (UTC) Injection-Info: mx04.eternal-september.org; posting-host="471490eb7dbb5112d18d2869517c5aa4"; logging-data="29983"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19EWPy8XQ7R+Se96ZCk3N/x" User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:12.0) Gecko/20120428 Thunderbird/12.0.1 In-Reply-To: <37e29245-591a-4325-b767-fdfe8d09a703@googlegroups.com> Cancel-Lock: sha1:4+k8wqajRlR0wBVVirTxPeOJlhw= Xref: csiph.com comp.lang.java.programmer:20200 On 09/12/2012 21:36, artik allegedly wrote: > Thank You very much! Honestly. > I started analyze Your code but it is partly to hard for me - I'm newbe in Java and it gives me error on invoking method "getWriter" in line: > Writer w = connector.getWriter() > and further exactly inside this method in line "else if( ! socket.isClosed() )". I'm going fight with this in next hours, and don't give up. > > One hour ago I write my code as simple as I can and in one thread - everything to avoid problem with synchronization. Effect on the server is still the same (several connection attempts after starting server when client was waiting for it) > I apologize that I'm putting my code again (I hope in good format) instead try to adapt new one from You immediately, but I want to understand this problem. Ah, shoot. I forgot the null check in the line you mention (btw, when you get an error, always say *what* error it is. Best is to post the stacktrace of the error; in this case it's obvious, but it's seldom that easy). Yes, the format is better this time. Also, you probably have solved your synchronisation issues for the time being. After the initial sleep, you should not go into the rest of the loop if you have been interrupted (being interrupted means "stop doing things", remember). Likewise, your last code is a regression from before, where you had the interruptedness as the loop condition (in the while clause), which is definitely right. In the latest form, AFAICS, the thread won't exit even if it is interrupted. As for the problem you mention: it is normal that you'd get several connection attempts. Do you think there's anything wrong with that? Client tries, server ain't there, client retries later. Seems okay to me. What you said before was that your app was crashing after a while in that case. I'm not sure why that is the case, but a candidate would be to close() the socket you've created when it fails to connect. I.e.: try { sock = new Socket(); sock.connect(new InetSocketAddress(address, 5000), 1000); out = new BufferedWriter( new OutputStreamWriter(sock.getOutputStream())); } catch (IOException e1) { if( sock != null ){ try { sock.close(); } catch( IOException ioex ){ ioex.printStacktrace(); } sock = null; } } -- DF.