Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Java (android) socket reconnection

Thread Tools

Java (android) socket reconnection

Daniele Futtorovic
Posts: n/a
On 09/12/2012 23:45, artik allegedly wrote:
> Thank you again for your advising - it is worth for me so much. When
> we back to your code and checking null, is my "improvement" your code
> good: @Override public synchronized Writer getWriter() throws
> IOException { if( disposed ){ throw new IOException( "Connector is
> closed" ); }
> if( socket != null && ! socket.isClosed() ){ assert writer != null;
> return writer; } else if( socket != null ){
> It's working but sometimes new null error happens in different
> place (some lines below - in my project it is 125th row of your
> code):
> this.socket = sc.socket();
> Here you are description: 12-09 22:06:52.982: E/AndroidRuntime(1387):
> FATAL EXCEPTION: Thread-9 12-09 22:06:52.982: E/AndroidRuntime(1387):
> java.lang.NullPointerException 12-09 22:06:52.982:
> E/AndroidRuntime(1387): at
> com.example.aj.siec.ConnectionsExample$1.getWriter (

12-09 22:06:52.982: E/AndroidRuntime(1387): at
> 12-09 22:06:52.982: E/AndroidRuntime(1387): at
> Beside this your proposition of code is almost perfect! Almost,
> because I can't reduce time of reconnection (like in my simple code
> using timeout for breaking attempt of connection). After long time
> waiting for server, after I turn on it - only one attempt (not like
> in my code over a dozen ) is taking but time (from the moment I turn
> on server) for waiting on connection takes about 50-70seconds with
> only waiting for set connection.
> I think my code beside point what you were show, is bad because I
> think it have works in schema: 1. server is switched off 2. client
> tries to connect to server but attempt fails 3. client closes socket
> and try again point.2 until pass to connect 4. only last (succesfull)
> attempt connects to server is initializing rest of connection.
> But in my "sad" example - it looks like all attempts from point are
> remember and when point 3 happend they try to connect again and
> immediately finish after connection - for the rest of the time only
> one of them (I mean last and (pass attempt) continues connection.
> Beside problem of strange attempt for connect after server woke up is
> that it is crush my application still - but the time is needed some
> longer than before.


Firstly, generally speaking, it is indeed important to have good code,
but it is at least just as important to have *code you understand*.
Because if you don't understand your code, then you won't be able to
debug or improve it, which is almost always a requirement.

The bits of code I offered you were mainly intended to give you (however
efficient) hints as to how to structure a program. That is, that you
should analyse (this is an abstract, mental exercise) what the processes
and actors are in the "things the program does", to regroup what belongs
together and separate what doesn't, and to then represent these using
classes. I don't know if that makes much sense; I reckon it doesn't.
What I want to say is that if you feel it's going over your head, you'd
probably be best advised to leave it aside for study when you have the
time, and stick to something you feel comfortable with.

Secondly, yes, the correction you made should be okay.

Thirdly, the NullPointerException NPE you mention seems really weird. I
don't see how that could happen if you've used the same code.

Fourth, I've used the SocketChannel method because I find it a bit
cleaner and because it's part of the NIO package, which is overall an
improvement over the older IO package. But indeed, a problem with that
is you can't set the socket timeout using that method, and your original
code was doing that, so it was actually a mistake to use it. If you want
to specify a connection timeout (what you called "time of
reconnection"), you should stick to the code you used previously (i.e.,
creating a new Socket() and then connect()ing it).

Lastly, I must admit I have trouble understanding exactly what you are
saying, but there's another point I'd like to address, rather. That is,
how often are you creating those polling/connection objects or threads?
Your original code seemed to do it on the press of a UI button. Are you
taking care of cleaning up the previous state if the button is pressed a
second time?

Reply With Quote
Posts: n/a
> 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;
> }
> }

I don't know why yesterday - No, but today Yes - it works perfect for me.
Thank You again
Reply With Quote
Posts: n/a
artik wrote:
>> try {
>> sock = new Socket();
>> sock.connect(new InetSocketAddress(address, 5000), 1000);
>> out = new BufferedWriter( new OutputStreamWriter(sock.getOutputStream()));
> > } catch (IOException e1) {
>> if( sock != null ){

Umm, no. This part belongs in a 'finally' block, not a 'catch' block. Oops.

>> try { sock.close(); } catch( IOException ioex ){
>> ioex.printStacktrace(); }

You need to format your code better. This is virtually unreadable.

>> sock = null;

With a 'final sock' variable and multiple try-catch blocks you can avoid the check for 'null'
and the reset to 'null'. (You probably don't need the reset now anyway.)

>> }
>> }

> I don't know why yesterday - No, but today Yes - it works perfect for me.

I doubt that very much. As written it is buggy.

Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Wireless not reconnection always. Big_Al Wireless Networking 1 03-15-2009 10:40 AM
Re: socket.unbind or socket.unlisten? - socket.error: (48, 'Addressalready in use') Laszlo Nagy Python 0 02-01-2009 07:37 AM
socket.unbind or socket.unlisten? - socket.error: (48, 'Addressalready in use') Laszlo Nagy Python 1 01-27-2009 05:05 PM
Re: socket.unbind or socket.unlisten? - socket.error: (48,'Address already in use') Jean-Paul Calderone Python 0 01-27-2009 01:41 PM
Automatic dial-up reconnection? A. J. Moss Computer Support 3 08-09-2003 03:45 PM