Re: Java (android) socket reconnection
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 (som=
e 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.Connect=
ionsExample$1.getWriter(ConnectionsExample.java:125)
12-09 22:06:52.982: E/AndroidRuntime(1387): at com.example.aj.siec.Connect=
ionsExample$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 c=
an'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 taki=
ng but time (from the moment I turn on server) for waiting on connection ta=
kes 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 o=
f connection.
But in my "sad" example - it looks like all attempts from point are remembe=
r 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