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


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

Re: Java (android) socket reconnection

Newsgroups comp.lang.java.programmer
Date 2012-12-09 14:45 -0800
References (1 earlier) <ka2c97$ag8$1@dont-email.me> <27f309f0-5118-4fa8-8b26-858d19af27de@googlegroups.com> <ka2k1v$l3$1@dont-email.me> <37e29245-591a-4325-b767-fdfe8d09a703@googlegroups.com> <ka323a$t8v$1@dont-email.me>
Message-ID <d5c6a0b4-a9da-4060-8c04-912d8368c415@googlegroups.com> (permalink)
Subject Re: Java (android) socket reconnection
From artik <olsztyn.arti@gmail.com>

Show all headers | View raw


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(ConnectionsExample.java:125)
12-09 22:06:52.982: E/AndroidRuntime(1387): 	at com.example.aj.siec.ConnectionsExample$Poller.run(ConnectionsExample.java:50)
12-09 22:06:52.982: E/AndroidRuntime(1387): 	at java.lang.Thread.run(Thread.java:1019)


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.

Regards,
Artik

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