Hi Guys
My application use socket to connect a remote sever to get data.
In my handset, both Wi-Fi and Mobile network data connection are enabled, by default device will choose using Wi-Fi connection. In this case, all network access can process normally
However, when I turn off Wi-Fi in system setting page and switch back to my application to start a new session for network access(it will use mobile network), I found that socket could not setup( constructor Socket(String hostname, int port) method does not return).
But if I kill the process then restart application, my application can communicate with server via mobile network normally
Any one know what's the reason for this?
In my application, each time I send a request to server, a new Socket object will be created.
public class SocketConnection implements IConnection{
private Socket sock;
/**
* Initialize the socket
*/
public SocketConnection(IConnectionConfig config) throws IOException, SecurityException{
sock = new Socket(config.getHostName(), config.getHostPort());
}
/**
* @see Connection#openInputStream()
*/
public InputStream openInputStream(){
try {
return sock.getInputStream();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* @see Connection#openOutputStream()
*/
public OutputStream openOutputStream(){
try {
return sock.getOutputStream();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* @see Connection#close()
*/
public void close(){
try {
sock.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Many thanks