Android device to PC's socket connection

21,594

Solution 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.

Solution 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/

Share:
21,594
gsmaker
Author by

gsmaker

Updated on October 14, 2020

Comments

  • gsmaker
    gsmaker over 3 years

    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.

  • hengxin
    hengxin almost 10 years
    After establishing the TCP connection, how should I push data from Android device to PC? Specifically, the device is able to listen to and accept a Socket on the side of PC, get its ObjectOutputStream, and write data into it. Then, what should PC do to receive the data? Thanks.
  • hengxin
    hengxin almost 10 years
    Get it through finally.