The socket does not throw an exception, although it cannot connect

I am trying to connect a socket to a nonexistent server, and I really don’t understand why the exception does not occur.

Here is my code:

public class TestSocket extends Activity { private static final String TAG = "TestSocket"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); BasicThread t = new BasicThread(); t.start(); } class BasicThread extends Thread { @Override public void run() { Log.d(TAG, "Before"); try { new Socket("42.42.42.42", 12345); Log.d(TAG, "Connected"); } catch (Exception e) { Log.d(TAG, "Exception"); } Log.d(TAG, "After"); } } } 

I also tried to use my own IP address while Wireshark was working, and first I got [SYN] from Android to my computer, and then [RST, ACK] from my Android computer (because it doesn’t listen to anything on this port), but I still don't get exceptions on Android.

Also Im testing on a physical phone (Nexus S), and I have internet permission in my manifest.

Why am I not getting an Exception ?

Edit:

More precisely, the conclusion that I get is

 D/TestSocket(17745): Before D/TestSocket(17745): Connected D/TestSocket(17745): After 

(not Exception )

+4
source share
3 answers

In the Socket constructor, it is generated when the host IP address cannot be determined, so I assume that since you are not passing a host name that needs to be resolved, another exception will be thrown instead. I believe that the exception does come from a class of URLs or one that does the resolution, and nowhere else.

The connect (..) method should throw an exception, but does not appear, as you say.

Edit: it is obvious that Android (some versions) are not working properly here, so this is probably a bug: http://code.google.com/p/android/issues/detail?id=6144 . This is not like the link refers to the emulator as I thought.

0
source

There are many things that can lead to a socket connection failure with an exception.

However, if the SYN message that the TCP protocol sends to start the connection process,

  • blocked by a firewall
  • routed through a borked network, or
  • redirected to any endpoint that does not respond,

then the TCP stack on the initiating machine will simply try again and try again. If you have a connection timeout, you will end up with an exception, but this can take a long time.


The fact that it works on an Android emulator, and not on a real device, simply means that they are implemented or configured differently. (For example, they can have different default connection timeouts ... emulators can be designed to fail in this scenario.)

The bottom line is that you need to make your code work on a real device. Find out how best to configure the device to set the connection timeout and verify that this works when talking to a non-existent server.

0
source

Your catch catch code does not catch IO exceptions. Try something like this

 try { // to get the ip address of the server by the name<br> InetAddress ip =InetAddress.getByName("example.com"); sock= new Socket(ip,Server.PORT); ps= new PrintStream(sock.getOutputStream()); ps.println(" Hi from client"); DataInputStream is = new DataInputStream(sock.getInputStream()); System.out.println(is.readLine()); }catch(SocketException e){ System.out.println("SocketException " + e); }catch(IOException e){ System.out.println("IOException " + e); } 
-2
source

All Articles