Firstly I know what your thinking, it's a strange question and it seems like it cannot be true but here me out on this one...
The project is a project which sends an array of bytes over a socket and also receives data from a socket. I am currently working on protection. I want to make sure that the user knows when the socket can't connect for whatever reason... You can view the connection code below.
So I have an android app which connects to a executable on a Windows Server computer in a office elsewhere in the world. The socket connects via the usual IP address and port number.
I am testing the app using a Android 5.0 (lollipop) phone...
Now enough of the boring stuff: If I close the server and turn it off completely and test this on wi-fi then the socket fails - which is correct. It cannot connect, and it will throw a SocketException. Which is fine and is working as expected. Now if I were to turn wi-fi off and use mobile data (I use o2 here in the sunny United Kingdom!) then an issue arises, using the same code, with the server still turned off, I do not throw a SocketException, instead the code simple thinks it has connected on a socket. The connection thread is below.
Thread StartConnection = new Thread()
{
@Override
public void run()
{
int dstPort = 10600;
socket = new Socket();
try
{
ipaddress = InetAddress.getByName(dstName);
}
catch (UnknownHostException uhe)
{
uhe.printStackTrace();
}
j = new InetSocketAddress(IPString , dstPort);
try
{
socket.setKeepAlive(true);
socket.setReuseAddress(true);
socket.setTcpNoDelay(true);
socket.setSoTimeout(5000);
socket.connect(j, 5000);
connected = true;
outputstream = socket.getOutputStream();
DataThread();
SocketError = false;
}
catch (SocketException se)
{
se.printStackTrace();
}
catch (IllegalArgumentException iae)
{
iae.printStackTrace();
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
catch (Exception e)
{
e.printStackTrace();
}
}
};
StartConnection.start();
socket.connect(j, 5000);
works fine no socket errors. No timeouts either.
outputstream = socket.getOutputStream();
also works. I get a result of "java.net.PlainSocketImpl$PlainSocketOutputStream@48e6f6e" which is also fine.
socket.getInputStream()
returns "java.net.PlainSocketImpl$PlainSocketInputStream@1ea305a5"