Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!us.feeder.erje.net!news2.arglkargh.de!news.swapon.de!eternal-september.org!feeder.eternal-september.org!mx04.eternal-september.org!.POSTED!not-for-mail From: Knute Johnson Newsgroups: comp.lang.java.programmer Subject: Re: Detection of socket connecting process, two thread problem Date: Fri, 25 Jan 2013 10:11:34 -0800 Organization: A noiseless patient Spider Lines: 71 Message-ID: References: <53a99ae7-6ff8-42d6-87f6-822a0310a475@googlegroups.com> <81jk5rtxpez7$.1vp8ezw6dkdfx$.dlg@40tude.net> <743afe02-c761-461a-87ba-4c61f143bd0b@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Fri, 25 Jan 2013 18:11:35 +0000 (UTC) Injection-Info: mx04.eternal-september.org; posting-host="aba33539224e5c782fe0c4053f7756fd"; logging-data="12970"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/W0SOadGDusjWxu0eYHaTK" User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:17.0) Gecko/20130107 Thunderbird/17.0.2 In-Reply-To: <743afe02-c761-461a-87ba-4c61f143bd0b@googlegroups.com> Cancel-Lock: sha1:uYeondkUxNzG9VWgOE6sfpFjM4E= Xref: csiph.com comp.lang.java.programmer:21710 On 1/25/2013 2:33 AM, artik wrote: > I'm newbie in Java and after several attempts and changing approaches i've decided to use synchronization like this below. I think it protects code (?) to use socket in the same time. Two threads using this procedures (disconnect and connect after) wait (if they need) when the second thread finished connecting and start to do the same: disconnect and connect. I can't find the way: don't doing disconnection-connection by the second thread when the first one started this just second before. > > When error connections occurs in the threads (reading/writing) then call > reconnection procedure insede it are: > {... > close_connection(); > set_connection(); > ... > } > which are below > > public synchronized void close_connection() { > try { > socket.shutdownInput(); > socket.shutdownOutput(); > socket.close(); > try { > Thread.sleep(500); > } catch (InterruptedException e1) { > e1.printStackTrace(); > } > } catch (IOException e1) { > // TODO Auto-generated catch block > e1.printStackTrace(); > } > } > > public synchronized boolean setconnection() { > boolean result=true; > socket = new Socket(); > try { > socket.connect(new InetSocketAddress(address, port), 500); > in = new BufferedReader(new InputStreamReader( > socket.getInputStream())); > out = new BufferedWriter(new OutputStreamWriter( > socket.getOutputStream())); > } catch (IOException e) > {result=false; > } > return result; > } > > Artik > I like to keep this sort of thing very simple. Divide up the connection handling part from the I/O part. Main thread Loop Handle connection Spawn service threads that handle I/O by passing socket Wait until all service threads are finished EndLoop End Main thread Service thread Loop Create readers and writers as appropriate Do I/O and process If error Close socket to stop all service threads Break out of loop End Loop End service thread -- Knute Johnson