Java Android设备到PC的socket连接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3880059/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 06:17:28  来源:igfitidea点击:

Android device to PC's socket connection

javaandroidsocketsconnection

提问by gsmaker

I am facing problem to establish a socket connection from android device to PC's a specific port like 8080. I just want to create a socket which will connect to the specific port and also write some data stream on that port.

我在建立从 android 设备到 PC 的特定端口的套接字连接时遇到问题,例如8080. 我只想创建一个连接到特定端口的套接字,并在该端口上写入一些数据流。

I have written some code for this purpose but the code is giving me an exception as:

我为此目的编写了一些代码,但代码给了我一个例外:

TCP Error:java.net.ConnectException:/127.0.0.1:8080-connection refused

I am giving my code as below:

我给我的代码如下:

private static TextView txtSendStatus;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        initControls();


        String sentence = "TCP Test #1n";
        String modifiedSentence;

        try {

            Socket clientSocket = new Socket("192.168.18.116", 8080);
            DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
            BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
            printScr("TCP Connected.");

            outToServer.writeBytes(sentence + 'n');
            modifiedSentence = inFromServer.readLine();
            printScr(modifiedSentence);
            printScr("TCP Success !!!");

            clientSocket.close();

        } catch (Exception e) {
           printScr("TCP Error: " + e.toString());
        }
    } 
    private void initControls()
    {
          txtSendStatus = (TextView)findViewById(R.id.txtSendStatus);
    }

    public static void printScr(String message)
    {
           txtSendStatus.append( "n" + message );
    }

Is there anyone who can tell me the answer? I am waiting for the right answer.

有谁能告诉我答案吗?我在等待正确的答案。

Best Regards, gsmaker.

最好的问候,gsmaker。

回答by Tseng

First off, if you try to connect to 127.0.0.1 from your device, it's only logical you can't. Because the 127.0.0.1 is the loopback interface and always points on the device itselfs.

首先,如果您尝试从您的设备连接到 127.0.0.1,那是合乎逻辑的。因为 127.0.0.1 是环回接口并且始终指向设备本身。

So if you connect to 127.0.0.1 from your PC it will connect with itself. If you call it on android it tries to connect with itself too.

因此,如果您从 PC 连接到 127.0.0.1,它将与自身连接。如果您在 android 上调用它,它也会尝试与自身连接。

And second: I think the only way you could do this is when you're using WLAN, only then you have IP based connection to the PC (correct me if I'm wrong). You can't connect to your PC using USB or Bluetooth.

第二:我认为你可以做到这一点的唯一方法是当你使用 WLAN 时,只有这样你才能与 PC 建立基于 IP 的连接(如果我错了,请纠正我)。您无法使用 USB 或蓝牙连接到您的 PC。

回答by Chris Stratton

If you are using wifi, you need to use the IP address of your PC on the wifi network. You can find this at the command line with ifconfig (linux) or ipconfig (windows)

如果您使用的是 wifi,则需要使用您电脑在 wifi 网络上的 IP 地址。您可以使用 ifconfig (linux) 或 ipconfig (windows) 在命令行中找到它

If you are using the usb adb connection, you can't exactly do this, but you can set up an adb port forward (see developer docs) from the PC to the phone, and have the pc connect to it's loopback interface and the port, which will be forwarded to an unprivileged port number on the phone where your application should be listening. You then have a TCP or whatever connection which you can push data over in either direction. But the PC has to be the initiator to set up the connection - adb does not support "reverse tethering" in which the phone initiates network-over-usb connections to the PC in the way that is supported for the android emulator.

如果您使用的是 usb adb 连接,则不能完全执行此操作,但是您可以设置一个 adb 端口转发(请参阅开发人员文档)从 PC 到手机,并让 PC 连接到它的环回接口和端口,它将被转发到您的应用程序应该侦听的电话上的非特权端口号。然后你有一个 TCP 或任何你可以在任一方向推送数据的连接。但是 PC 必须是建立连接的发起者 - adb 不支持“反向网络共享”,其中手机以 Android 模拟器支持的方式启动与 PC 的网络连接。

回答by Nelson Ramirez

Your server needs to be on the device and the client needs to be on the computer. You'll need to have adb forward the port you want to connect to the device. After your connection is established, you'll be able to communicate between them normally.

您的服务器需要在设备上,客户端需要在计算机上。您需要让 adb 转发要连接到设备的端口。建立连接后,您将能够在它们之间正常通信。

I wrote up a full explanation here http://qtcstation.com/2011/03/connecting-android-to-the-pc-over-usb/

我在这里写了一个完整的解释http://qtcstation.com/2011/03/connecting-android-to-the-pc-over-usb/