开发者

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();


        Stri开发者_如何学JAVAng 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.


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/


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜