i have sort of a fix. if there is no service 'on the other end' we
will never ever receive an EOL. so the following code will work, if we
assume that services always start by saying something, followed by an
newline. (i don't know any regular examples that don't start by saying
hi.)
it is not so pretty, but i have no clue how to bypass this 3G
glitch...
try {
InetAddress address = InetAddress.getByName(dns);
SocketAddress endpoint = new InetSocketAddress(address, port);
s.connect(endpoint, 1000);
final BufferedReader in = new BufferedReader(new InputStreamReader(
s.getInputStream()));
// because on my G1 3G is weird (it always sets up a connection to
// everything) we need to talk through the connection. but the
// readLine is blocking, and we don't want that either. using
// 'special magic' like future and executors we can let the readline
// timeout after 500 milliseconds... (i hope)
Future<String> future = executor.submit(new Callable<String>() {
public String call() {
try {
return in.readLine();
} catch (IOException e) {
return null;
}
}
});
try {
input = future.get(500, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
} catch (TimeoutException e) {
} catch (ExecutionException e) {
}
in.close();
s.close();
if (input == null) {
return false;
} else {
return true;
}
} catch (UnknownHostException e) {
return false;
} catch (IOException e) {
return false;
}
On Oct 14, 10:30 am, truthtrap <[email protected]> wrote:
> guys and girls,
>
> i am trying to test the availability of a service with a socket. the
> code is quite simple
> try {
> InetAddress address = InetAddress.getByName(dns);
> s = new Socket();
> try {
> SocketAddress endpoint = new
> InetSocketAddress(address, port);
> s.connect(endpoint, 1000);
> s.close();
>
> return true;
> } catch (IOException e) {
> return false;
> }
> } catch (UnknownHostException e) {
> return false;
> }
>
> it works in the emulator and i thought everything was ok. but it does
> not work on my device (ADP with 1.6.) actually it does work, but not
> with 3G.
>
> the socket.connect blocks when the device is in wifi mode, as
> expected. but when on 3G it just continues, as if a connection has
> been made.
>
> (the tested case is port 80 on an amazon ec2 instance.)
>
> can anyone either tell me why this happens so i can fix. or perhaps
> there is an alternative to testing service connections for
> availability.
>
> thanks,
> jurg.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---