Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sockets 开放式Android仿真器网络局域网_Sockets_Networking_Android Emulator_Ip_Ping - Fatal编程技术网

Sockets 开放式Android仿真器网络局域网

Sockets 开放式Android仿真器网络局域网,sockets,networking,android-emulator,ip,ping,Sockets,Networking,Android Emulator,Ip,Ping,我使用了Eclipse程序 如何将Android Emulator打开到网络Ip, 我想用我的应用PING另一个Ip 谢谢! 我发布了我的应用程序代码,没有错误,运行的sApp启动没有问题,但当我将IP放入EditText字段时,应用程序崩溃 这是我的代码: package com.example.clientping; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamRe

我使用了Eclipse程序

如何将Android Emulator打开到网络Ip, 我想用我的应用PING另一个Ip

谢谢! 我发布了我的应用程序代码,没有错误,运行的sApp启动没有问题,但当我将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" );
        }

}     

}

提供您试图在应用程序内部连接的计算机的IP地址。在尝试访问该系统之前,请禁用您尝试连接的计算机的防火墙和防病毒软件。通过使用命令提示符ping另一台计算机来测试连接。您必须在mainifest文件中使用此权限使用权限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;
}

}

您不能这样做,因为仿真器不支持ping

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

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

这也意味着像仿真器这样的“常规”应用程序无法做到这一点。
抱歉。

我这样做了,但它不起作用,我已经读到AndroidSimulator与互联网的其他部分(以及IP)隔离不,我只是想“ping”IP andressI可能我用cmd来做,但不是用我的AppHi Ajeesh,我创建了这个代码,我想尝试这个应用No,当我输入Ip地址时,应用程序会崩溃并感谢您的帮助数字,因此无法精确ping,也无法使用ICMP协议(ping使用)。您仍然可以尝试通过TCP或UDP进行连接,以检查是否可以做到这一点。这将为您提供基本的连接信息(但不是通过ICMP报告的跳数、延迟等)。