Android设备到PC的套接字连接

Android设备到PC的套接字连接

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

问题描述

我面临的问题,建立从Android设备的套接字连接到PC的像8080我只是想创建一个插座,将连接到特定的端口,并写在该端口上的一些数据流的特定端口。

我已经写了一些code用于这一目的,但code是给我一个例外,TCP错误:java.net.ConnectException:/127.0.0.1:8080 - 连接被拒绝

我给我的code如下:

私有静态的TextView txtSendStatus;

  / **第一次创建活动时调用。 * /
@覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);
    initControls();


    串句=TCP测试#1N;
    字符串modifiedSentence;

    尝试 {

        插座ClientSocket的=新的Socket(192.168.18.116,8080);
        DataOutputStream类outToServer =新DataOutputStream类(clientSocket.getOutputStream());
        的BufferedReader inFromServer =新的BufferedReader(新的InputStreamReader(clientSocket.getInputStream()));
        printScr(TCP连接。);

        outToServer.writeBytes(句+'N');
        modifiedSentence = inFromServer.readLine();
        printScr(modifiedSentence);
        printScr(TCP成功!);

        clientSocket.close();

    }赶上(例外五){
       printScr(TCP错误:+ e.toString());
    }
}
私人无效initControls()
{
      txtSendStatus =(TextView中)findViewById(R.id.txtSendStatus);
}

公共静态无效printScr(字符串消息)
{
       txtSendStatus.append(N+消息);
}
 

是否有任何人谁可以告诉我答案?我在等待正确的答案。

最好的问候,gsmaker

解决方案

如果您使用的是无线网络,则需要使用WiFi网络中的PC的IP地址。你可以找到这个在命令行用ifconfig(Linux)或IPCONFIG(视窗)

如果您使用的是USB ADB连接,你不能完全做到这一点,但你可以建立一个ADB端口转发从PC到手机(见的开发文档),并有将PC连接到它的loopback接口和港口,这将被转发到一个非特权端口号,你的应用程序应该被监听的电话。那么你有一个TCP或任何连接,你可以在任何一个方向上推数据。但是,PC必须是发起建立连接 - 亚洲开发银行不支持反向圈养在手机启动到PC中所支持的Andr​​oid模拟器的方式网络过USB连接。

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 asTCP 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

解决方案

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)

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的套接字连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 01:47