Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android device to PC's socket connection

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.

like image 433
gsmaker Avatar asked Oct 07 '10 08:10

gsmaker


2 Answers

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.

like image 56
Chris Stratton Avatar answered Sep 23 '22 23:09

Chris Stratton


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/

like image 42
Nelson Ramirez Avatar answered Sep 24 '22 23:09

Nelson Ramirez