user3742672
user3742672

Reputation: 49

Android java How to connect to server with different network/ip address

How to connect to server with different IP address

I really don't know what to do...

Let me explain first.

i have a client and server that works great when sending messages between the two if i am on the same network as my computer.

i have an android device and i would like to wish when ever i am placed far away and when i will click some button it will send message to server even if i am not on the same network.

Client

try {

                client = new Socket("IpAddress", 4444);
                BufferedReader in = new BufferedReader(
                        new InputStreamReader(client.getInputStream()));
                printlng = new PrintWriter(client.getOutputStream());
                printlng.println(mlng);
                printlng.flush();
                while (true) {
                    if ((Response= in.readLine()) != null) {
                        Log.i("Response:", Response);
                        dlng = Double.valueOf(Response);
                        System.out.println(dlng);

                        break;
                    }
                }

Server:

public static void main(String[] args) {

    try {
        serverSocket = new ServerSocket(4444); // Server socket

    } catch (IOException e) {
        System.out.println("Could not listen on port: 4444");
    }

    System.out.println("Server started. Listening to the port 4444");

    while (true) {
        try {

            clientSocket = serverSocket.accept(); // accept the client
            inputStreamReader = new InputStreamReader(
                    clientSocket.getInputStream());
            bufferedReader = new BufferedReader(inputStreamReader); // get
                                                                    // the
            // client


            PrintWriter out = new PrintWriter(
                    clientSocket.getOutputStream(), true);
            InputStream inputStream = new ByteArrayInputStream(
                    bufferedReader.readLine().getBytes(
                            Charset.forName("UTF-8")));
            BufferedReader bufferedReader2 = new BufferedReader(
                    new InputStreamReader(inputStream));

            String output = bufferedReader2.readLine();
            System.out.println(output.toString());
            out.println(output.toString());
            out.flush();
            out.close();

            inputStreamReader.close();
            clientSocket.close();

        } catch (IOException ex) {
            System.out.println("Problem in message reading");
        }
    }

}

Upvotes: 3

Views: 2900

Answers (2)

greenapps
greenapps

Reputation: 11214

You have to find out the external/internet ip address of the pc where your server is running on. You can do that on that pc with http://whatismyip.com. Use the obtained ip in your client. But before it really can work you have to configure the router where your pc is connected to to forward the used port to the lan ip address of your pc.

Upvotes: 1

ben75
ben75

Reputation: 28706

If everything works fine when you are on the same LAN : the problem is probably coming from some firewall rule not accepting connection on port 4444 when coming from a non local IP.

So, either change the port of the server (if not used yet, port 80 is probably a good choice since there is more chance that connections will be allowed), either change your firewall rules.

Upvotes: 0

Related Questions