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

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

问题描述

我面临的问题,建立一个套接字连接从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,你需要使用你的电脑在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连接,你不能这样做,但您可以设置一个adb端口从PC到手机转发(见开发人员文档),并使pc连接到它的环回接口和端口,它将转发到您的应用程序应用程序上的手机上的无特权的端口号听力。然后你有一个TCP或任何连接,你可以推送数据在任一方向。但是PC必须是启动器来设置连接 - adb不支持反向绑定,其中电话通过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的socket连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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