Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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");
        }
    }

}
like image 743
user3742672 Avatar asked Apr 10 '26 19:04

user3742672


1 Answers

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.

like image 117
greenapps Avatar answered Apr 13 '26 08:04

greenapps



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!