Android device to PC's socket connection

醉酒当歌 提交于 2020-01-12 08:05:14

问题


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

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.


回答1:


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.




回答2:


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.

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




回答3:


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.

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.

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.



来源:https://stackoverflow.com/questions/3880059/android-device-to-pcs-socket-connection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!