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


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

Re: Java (android) socket reconnection

Path csiph.com!usenet.pasdenom.info!gegeweb.org!eternal-september.org!feeder.eternal-september.org!mx04.eternal-september.org!.POSTED!not-for-mail
From Eric Sosman <esosman@comcast-dot-net.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: Java (android) socket reconnection
Date Sun, 09 Dec 2012 11:09:28 -0500
Organization A noiseless patient Spider
Lines 51
Message-ID <ka2d3s$gdl$1@dont-email.me> (permalink)
References <9a8716eb-5842-45d8-b62e-193122cd863e@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
Injection-Date Sun, 9 Dec 2012 16:09:32 +0000 (UTC)
Injection-Info mx04.eternal-september.org; posting-host="ffb8f7085759b339c1002252b48331a4"; logging-data="16821"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19/s1DPaaNpU2F+5BJZLEhQ"
User-Agent Mozilla/5.0 (Windows NT 5.1; rv:17.0) Gecko/17.0 Thunderbird/17.0
In-Reply-To <9a8716eb-5842-45d8-b62e-193122cd863e@googlegroups.com>
Cancel-Lock sha1:WVVkElUI4y5vdLkxqJMO35niriE=
Xref csiph.com comp.lang.java.programmer:20191

Show key headers only | View raw


On 12/9/2012 10:06 AM, artik 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?
> [... code snipped; see up-thread ...]

     One problem (there may be others) is that your two threads
both use the `sock' variable without synchronization.  Let's
call the threads T (the "transmitter") and C (the "connector"),
and imagine the following scenario:

	T: writes to `sock', gets I/O error
	T: sets `sock' null in response to the error
	C: sees `sock' null, sets `sock = new Socket()'
	C: performs `sock.connect()'
	T: sees `sock' non-null and writes to it

That's presumably what you intended, but since there's no
synchronization something like this might happen instead:

	T: writes to `sock', gets I/O error
	T: sets `sock' null in response to the error
	C: sees `sock' null, sets `sock = new Socket()'
	T: sees `sock' non-null, writes, gets error (not connected)
	T: sets `sock' null in response to second error
	C: calls `sock.connect()' and gets NullPointerException

     The problem is that either thread can try to use `sock'
while the other is in the middle of a sequence of steps that
make `sock' temporarily unusable.  You must use synchronization
to ensure that neither thread touches `sock' while the other
is manipulating it.  (You must use synchronization for some
other, subtler reasons, too.)

     Among the other possible problems: It seems odd that after
an I/O error you just abandon the Socket without calling close()
on it.  This looks very much like a resource leak that could
eventually bring down your program even if nothing else does.
(Or maybe not: I haven't studied it deeply.)

-- 
Eric Sosman
esosman@comcast-dot-net.invalid

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next 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