开发者

android socket communication through internet

开发者 https://www.devze.com 2023-04-07 02:16 出处:网络
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.

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开发者_JAVA技巧 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.


Ok, i figured out it was my firewall.


When you use a real Android phone as Internet Remote device, don't you have to set up your WiFi router, connected to your PC (or Android), for Port Forwarding? Then you give your Android Client the PC Server's External IP Address and the Server Port Number. Only then, provided the Port Forwarding works on the router, your remote Android Client (on SIM) can communicate with your PC Server connected to your router.

0

精彩评论

暂无评论...
验证码 换一张
取 消