简体   繁体   中英

Sending a string between two devices through a socket using TCP when server is on a private network

I am trying to send a string (and later also an image) from one Android app, to another Android app when each app is on a different device and the receiving device is on a private Wifi Network. (The server is listening on a private IP: 192.xx.xx.xx). This code works when the server is connected to a public IP, but not private, like I want.

The code "SendString" is one Android app on one phone. And the code "AppListener" is on the other phone (this is the server). I am able to send the string from SendString to AppListener when AppListener is connected to a public Wifi IP address.

"SendString", which is sending a string to "AppListener":

(the string that I am sending "applicationName" is getting passed to "SendString" from a different activity in the project)

public class SendString extends AsyncTask<String, String, String> {

@Override
protected String doInBackground(String... params) {

    Log.d("tag_name", "Entered AsyncTask");

    String applicationName = params[0];


    // SEND APPLICATION NAME AND ICON TO OTHER APP

    try {

        Log.d("tag_name", "TRY TO SEND STRING");
        Socket socket = new Socket("192.168.0.26", 1755);
        DataOutputStream DOS = new DataOutputStream(socket.getOutputStream());
        DOS.writeUTF(applicationName);
        Log.d("tag_name", "Application Name Sent!");
        socket.close();

    }

    catch (Exception e){

        Log.d("tag_name", "Did not send string");

    }

    return null;
}

}

The "SendString" code is getting stuck at the print statement "Try to Send String" and it is not able to connect to the IP of the other phone right after that print statement.

Here is the other Android app code (on the server) that is listening for a port connection, and then it should be receiving the string from "SendString".

public class AppListener extends AsyncTask<String, String, String> {



@Override
protected String doInBackground(String... uri) {

    String msg_received = null;

    System.out.println("LISTENING FOR LAST INSTALLED APP");

    try {


        System.out.println("TRY");
        ServerSocket socket = new ServerSocket(1755);
        System.out.println("Connect to Socket and listening");
        Socket clientSocket = socket.accept();       //This is blocking. It will wait.
        System.out.println("This should print after connection");
        DataInputStream DIS = new DataInputStream(clientSocket.getInputStream());
        msg_received = DIS.readUTF();

        System.out.println("Message from server" + msg_received);

        clientSocket.close();
        socket.close();
    } catch (Exception e) {
        System.out.println("Did not connect to SendString");
    }
    System.out.println("Return Statement is Reached");
    return msg_received;
}
}

The problem is that it seems to be getting stuck at the line

Socket clientSocket = socket.accept();       //This is blocking. It will wait.

Does anyone know how to make this connection work when my server is connected to a private network?

In this code line:

Socket socket = new Socket("192.168.0.26", 1755);

You should to replace the private IP 192.168.0.26 for a public IP, you can find the public IP entering in one of several network services like this

Also you need validate that 1755 port is open to incoming TCP connections in your firewall or in you router configuration.

Hope it hepls.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM