Android 设备到 PC 的套接字连接 [英] Android device to PC's socket connection

查看:26
本文介绍了Android 设备到 PC 的套接字连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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

我的代码如下:

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.

最好的问候,gsmaker.

Best Regards, gsmaker.

推荐答案

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

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)

如果您使用的是 usb adb 连接,则不能完全这样做,但您可以设置从 PC 到手机的 adb 端口转发(请参阅开发人员文档),并让 pc 连接到它的环回接口和端口,它将被转发到您的应用程序应该监听的电话上的非特权端口号.然后,您将拥有一个 TCP 或任何可以向任一方向推送数据的连接.但是 PC 必须是建立连接的发起者 - adb 不支持反向网络共享",其中手机以 android 模拟器支持的方式启动网络通过 USB 连接到 PC.

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.

这篇关于Android 设备到 PC 的套接字连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆