开发者

Connecting Android Emulator to a port locally

I am having nothing but problems with this simple task. I have my android emulator built in Eclipse running an Emulator, the application itself is built and working to a point.

In the interface the user enters an IPAddress and a Port to connect to and writes a simple message to send. A button labled send is clicked to pass the details.

In the main class I have a button click listener, that pulls the three fields and attempts to use them to establish a connection to the specific port for this example 4444.

The problem I am having is when I attempt to create a socket or an HTTP connection I am unable to do so. I have read many of the tutorials on Java/Oracle/Droid developer websites, somthing is eluding me and the application unexpectidly stops.

What I am asking you for is a simple example or suggestion in leymans terms for establishing a link via sockets or HTTP, I am not worried about sending the message yet, I simply need to create that connection and confirm that it works.

I am at my ends wits and have spent the best part of two days attempting numerous ways. So I kindly request your help!!

   private OnClickListener sendClickListener = new OnClickListener(){
    public void onClick(View arg0) {
    //EditText dstName = (EditText) findViewById(R.id.destinationAddress);
    //EditText dstPort = (EditText) findViewById(R.id.destinationPort);
    //address = dstName.toString();
    // port = Integer.parseInt(dstPort.toString()); PROBLEM         
        //InetAddress server;
        //try {
        //  server = Inet4Address.getByName("127.0.0.1");
        //  Socket clientsocket = new Socket(server, 4444);
        //} catch (IOException e) {
     开发者_开发问答   //  // TODO Auto-generated catch block
        //  e.printStackTrace();
        //  finish();
        //} 
    }
    };

I have posted some code in its current state it shows an accumulation of attempts to create a connection.

Kind Regards

Simon


Try 10.0.2.2 instead of 127.0.0.1 since 127.0.0.1 refers to the emulator itself which is probably not what you want while 10.0.2.2 is hard wired in the emulator to refer to the host machine of the emulator (your PC).

Have a look at the Emulator Networking Guide for more information about this.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜