0

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 ?

Nabin
  • 11,216
  • 8
  • 63
  • 98
Kurei
  • 1
  • 1
  • You are not writing to the server. just declaring the socket. You have to write something where you have " //Do something " – Mike Docherty Oct 28 '14 at 17:29
  • The router may be the issue. It may be blocking the port. Configure `port forwaring` on your router for port 2009 :) – Unknownweirdo Oct 28 '14 at 17:31
  • You're trying to write it in quite a low level. Is there any reason not to use HTTP protocol? – cliffroot Oct 28 '14 at 17:32
  • MikeDocherty : The program don't display "Connexion successful !" so it will not execute the // DO SOMETHING parts, am i right ? ---Losin'Me : I take note of this, i'll write if this is the solution in this post. ---cliffroot : i don't know the http protocol, i'll try it if the socket solution continue to bug – Kurei Oct 28 '14 at 19:23

0 Answers0