将 Android 模拟器连接到本地端口

发布于 2024-10-30 07:57:54 字数 1212 浏览 2 评论 0原文

对于这个简单的任务,我除了遇到问题之外什么都没有。我在 Eclipse 中构建了 Android 模拟器,运行模拟器,应用程序本身已构建并运行到一定程度。

在界面中,用户输入 IP 地址和要连接的端口,并写入要发送的简单消息。单击标记为“发送”的按钮以传递详细信息。

在主类中,我有一个按钮单击侦听器,它提取三个字段并尝试使用它们建立与本示例 4444 的特定端口的连接。

我遇到的问题是当我尝试创建套接字或 HTTP 时连接我无法这样做。我已经阅读了 Java/Oracle/Droid 开发人员网站上的许多教程,但有些东西让我困惑,并且应用程序意外停止。

我要求您提供一个简单的示例或建议,用莱曼术语来说,通过套接字或 HTTP 建立链接,我并不担心发送消息,我只需要创建该连接并确认它是否有效。

我已经不知所措,花了两天的大部分时间尝试了多种方法。所以我恳请您的帮助!

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云淡月浅 2024-11-06 07:57:54

尝试使用 10.0.2.2 而不是 127.0.0.1,因为 127.0.0.1 指的是模拟器本身,这可能不是您想要的,而 10.0.2.2 在模拟器中硬连线以指代模拟器的主机(您的 PC)。

有关详细信息,请参阅模拟器网络指南

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文