打开Android模拟器网络局域网

时间:2014-01-10 15:51:36

标签: sockets networking android-emulator ip ping

我使用过Eclipse程序。

如何将我的Android模拟器打开到网络IP, 我想用我的应用程序PING另一个Ip。

谢谢! 我发布了我的应用程序代码,没有错误,Run的应用程序启动没有问题,但是当我将IP放在EditText字段中时,应用程序崩溃,

这是我的代码:

package com.example.clientping;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button button = (Button)findViewById(R.id.button1);
    button.setOnClickListener(this);
}

public void onClick(View v) { 
        // TODO Auto-generated method stub

        TextView text = (TextView)findViewById(R.id.textView1);
        EditText textField = (EditText) findViewById(R.id.editText1); 

        if (textField.getText().toString().length() > 3)
        {
            String host = textField.getText().toString();
            String retorno = "";

            text.setTextColor(0xff0000ff);
            text.setText("Connecting...");

            try {
                Socket s = new Socket(host, 80);                   
                //outgoing stream redirect to socket
                OutputStream out = s.getOutputStream();

                PrintWriter output = new PrintWriter(out);
                  // send an HTTP request to the web server
                  output.println("GET / HTTP/1.1");
                  output.println("Host: " + host + ":80");
                  output.println("Connection: Close");
                  output.println();
BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));

                    // read the response
                      boolean loop = true;
                      StringBuilder sb = new StringBuilder(8096);
                      while (loop) {
                        if (in.ready()) {
                          int i = 0;
                          while (i != -1) {
                            i = in.read();
                            sb.append((char) i);
                          }
                          loop = false;
                        }
                      }
                      retorno = sb.toString();

                //Close connection
                s.close();

                text.setTextColor(0xff0000ff);
                text.setText("Your server runs: \n" 
                        + retorno );                    

            } 

                catch (UnknownHostException e) {
                // TODO Auto-generated catch block
                text.setTextColor(0xffff0000);
                text.setText("Error! The Host or IP is unknown." );
            } 

                catch (IOException e) {
                // TODO Auto-generated catch block
                text.setTextColor(0xffff0000);
                text.setText("Unknown error. Check your internet connection!" );
            }               

        } else {
            text.setTextColor(0xffff0000);
            text.setText("Error! Please type your host or IP" );
        }

}     

}

2 个答案:

答案 0 :(得分:0)

在应用程序中提供您尝试连接的计算机的IP地址。禁用防火墙&在尝试访问该系统之前尝试连接的计算机的防病毒软件。只需使用命令提示符ping另一台计算机即可测试连接。您必须在mainifest文件中使用此权限 uses-permission android:name =“android.permission.INTERNET”

以下代码正常运行,

package com.example.pingapp;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

import android.os.Bundle;
import android.os.StrictMode;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener
{

TextView text=null;
EditText textField=null;
Button button=null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();

    StrictMode.setThreadPolicy(policy); 

    text = (TextView)findViewById(R.id.textView1);
    textField = (EditText) findViewById(R.id.editText1); 
    button = (Button)findViewById(R.id.button1);

    button.setOnClickListener(this);
}

@Override
public void onClick(View v) 
{
    switch(v.getId())
    {
        case R.id.button1:
            if (textField.getText().toString().length() > 3)
            {
                String host = textField.getText().toString();                  
                String retorno = "";

                text.setTextColor(0xff0000ff);
                text.setText("Connecting...");

                try {
                    Socket s = new Socket(host, 8080);                          
                    //outgoing stream redirect to socket
                    OutputStream out = s.getOutputStream();

                    PrintWriter output = new PrintWriter(out);
                      // send an HTTP request to the web server
                    output.println("GET / HTTP/1.1");
                    output.println("Host: " + host + ":80");
                    output.println("Connection: Close");
                    output.println();

                    BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));

                        // read the response                       
                    StringBuilder sb = new StringBuilder(8096);                        
                    int i=0;
                    while ((i = in.read()) != -1) 
                    {                           
                        sb.append((char) i);
                        Log.d("Values: ", ""+sb.toString());
                    }               
                    retorno = sb.toString();

                    //Close connection
                    s.close();

                    text.setTextColor(0xff0000ff);
                    text.setText("Your server runs: \n"+ retorno );     
                } 

                    catch (UnknownHostException e) {
                    // TODO Auto-generated catch block
                    text.setTextColor(0xffff0000);
                    text.setText("Error! The Host or IP is unknown." );
                } 

                    catch (IOException e) {
                    // TODO Auto-generated catch block
                    text.setTextColor(0xffff0000);
                    text.setText("Unknown error. Check your internet connection!" );
                }               

            } else {
                text.setTextColor(0xffff0000);
                text.setText("Error! Please type your host or IP" );
            }
        break;
    }

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

答案 1 :(得分:0)

你不能这样做,因为模拟器不支持ping。

更具体地说,它实现了一个NAT防火墙,它将来宾系统与真实主机网络分开,并将以太网数据包(发送到虚拟网络适配器)转换为BSD套接字/ WinSock库调用

要正确实现ping(即使用BSD套接字生成/接收PING数据报),程序需要具有root权限。这就是为什么你的ping'可执行文件是' setuid'在Linux上。

这也意味着没有办法让常规的'像模拟器这样的应用程序。 遗憾。