I want to create an application on android that checks if user is in the server database. At this moment, i just try to make connection work.
This is my client code :
public class Client {
public void testConnexion() {
new LoadTesting().execute();
}
class LoadTesting extends AsyncTask<String, String, String> {
private final String HOST_ADDRESS = "23.248.21.91";
private final Integer HOST_PORT = 2009;
@Override
protected String doInBackground(String... params) {
Socket socket;
try {
Log.d("CASERVER", "Connexion...");
InetAddress srvAddress = InetAddress.getByName(HOST_ADDRESS);
socket = new Socket(srvAddress, HOST_PORT);
Log.d("CASERVER", "Connexion successful !");
// DO SOMETHING
socket.close();
} catch (IOException e) {
e.printStackTrace();
Log.w("CASERVER", e.getMessage(), e);
}
return null;
}
}
}
And the server part
public class Server {
public static final int HOST_PORT = 2009;
public static void main(String[] args) {
ServerSocket serverSocket;
Socket socketDuServeur;
try {
serverSocket = new ServerSocket(HOST_PORT);
socketDuServeur = serverSocket.accept();
// DO SOMETHING
serverSocket.close();
socketDuServeur.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
I have add those permission
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
My server is on my macbook connected to my wifi. I've got two Eclipse Window : one for android and one for the server.
The test :
- 1 Launch the server, the server is waiting for connection
- 2 Launch the application on the mobile device and try to connect the server
I expect the result :
Connexion...
Connexion successful ! (Or an error)
I've got the following Log result instead :
Connexion...
No error, no successful. Just nothing... When i shut down manually the server (with another client on laptop) i've got EHOSTUNREACH error (device can't found the server)
Can you help me ?