Re: Java (android) socket reconnection
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(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.
Artik,
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?
--
DF.