简体   繁体   English

通过互联网进行android套接字通信

[英]android socket communication through internet

I'm experimenting with socket communication between android and windows. 我正在尝试在android和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. 一切正常,直到我使用10.0.2.2地址,该地址是运行仿真器的计算机的环回。 But if i give any other address to the Socket constructor the connection is timing out. 但是,如果我将任何其他地址提供给Socket构造函数,则连接将超时。 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. 我也在寻找Java内置函数,而不是将两个字节数组放在一起的concatenateArrays函数。

Edit: 编辑:

Sorry, maybe i not provided enough information. 抱歉,也许我没有提供足够的信息。 I have already tried my external ip used for the internet connection and my LAN ip. 我已经尝试过用于互联网连接的外部IP和局域网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. 因此,如果我写“ 192.168.1.101”或互联网服务提供商提供的IP代替“ 10.0.2.2”,则我无法连接。

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. Emulator使用与计算机相同的网络,因此它将能够将其路由到计算机。 But for your phone to connect with your computer, you have to give a different IP, which is basically the IP of the computer. 但是,为了使手机与计算机连接,您必须指定一个不同的IP,它基本上是计算机的IP。

I am guessing you are using some shared Network, and getting this (10.0.2.2) IP. 我猜您正在使用一些共享的网络,并获得此(10.0.2.2)IP。 Your computer should be directly connected to Internet in order for this to work from phone. 您的计算机应直接连接到Internet,以便通过电话工作。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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