简体   繁体   中英

android socket communication through internet

I'm experimenting with socket communication between android and windows. Everything works fine till i use the 10.0.2.2 address which is the loopback to the computer on which the emulator is running. But if i give any other address to the Socket constructor the connection is timing out. My goal is to communicate between my phone and my computer through the internet. I also tried it on my phone, so i don't think that it's a firewall problem. Here is my code:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    try {
        clientSocket = new Socket("10.0.2.2", 48555);
        Log.d("Offdroid", "socket connected");
    } catch (UnknownHostException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        System.out.println(e.toString());
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        System.out.println(e.toString());
    }
}

public void connectServer(View button) {
    try {
        String message = "shutdown";
        byte[] messageBytes = message.getBytes("US-ASCII");
        int messageByteCount = messageBytes.length;
        byte[] messageSizeBytes = new byte[2];
        messageSizeBytes = intToByteArray(messageByteCount);

        byte[] sendBytes = concatenateArrays(messageSizeBytes, messageBytes);

        Log.d("Offdroid", Integer.toString(messageSizeBytes.length));

        clientSocket.setReceiveBufferSize(16);
        clientSocket.setSendBufferSize(512);
        OutputStream outStream = clientSocket.getOutputStream();
        //InputStream inStream = clientSocket.getInputStream();

        outStream.write(sendBytes, 0, sendBytes.length);
    } catch(Exception EX) {
        Log.e("Offdroid", EX.getMessage());
    }
}

I'm also looking for a java built in function instead of the concatenateArrays function which simply put two byte array together.

Edit:

Sorry, maybe i not provided enough information. I have already tried my external ip used for the internet connection and my LAN ip. Port on router is forwarded to my computer. So if i write "192.168.1.101" or the ip given by the internet service provider in place of "10.0.2.2", than i cannot connect.

Edit:

Ok, i figured out it was my firewall.

Emulator takes uses the same network as that of your computer, so it will be able to route it to the computer. But for your phone to connect with your computer, you have to give a different IP, which is basically the IP of the computer.

I am guessing you are using some shared Network, and getting this (10.0.2.2) IP. Your computer should be directly connected to Internet in order for this to work from phone.

好的,我发现这是我的防火墙。

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