0

I used Eclipse program.

How I can open my Android Emulator to Network Ip, I want to PING another Ip with my App.

thanks! I post my App's code, there aren't errors, the Run'sApp starts without problems, but when I put the IP in EditText field the App crashes,

This is my code:

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 Answers2

0

Give the IP Address of the computer which you are trying to connect inside your Application. Disable the firewall & Antivirus of the computer which you are trying to connect before trying to access that system. Test the connection by just pinging another computer using command prompt. You have to use this permission in your mainifest file uses-permission android:name="android.permission.INTERNET"

This below code is working,

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

}
Ajeesh
  • 1,572
  • 3
  • 19
  • 32
  • I did this, but it does not work, I have read that the Androidemulator is shielded from the rest of the internet(and IP too) –  Jan 10 '14 at 15:58
  • No, I just to want to "ping" IP andress –  Jan 10 '14 at 16:01
  • I's possible I do it with cmd , but not with my App –  Jan 10 '14 at 16:02
  • Hi Ajeesh, I founded this code, I want to try this App http://stackoverflow.com/questions/6778103/a-simple-socket-app-in-android-needs-some-special-permissions-to-connect-through –  Jan 10 '14 at 16:10
  • No, whe I put the Ip adress the application crashes –  Jan 10 '14 at 16:21
  • 01-10 16:31:51.220: E/ActivityManager(61): Reason: keyDispatchingTimedOut –  Jan 10 '14 at 16:34
  • Sorry I need 10 Reputation to add image –  Jan 10 '14 at 16:42
  • I putted in my post the log cat's errors, the code it's the same in this post: http://stackoverflow.com/questions/6778103/a-simple-socket-app-in-android-needs-some-special-permissions-to-connect-through –  Jan 10 '14 at 16:44
  • Ajeesh, I'm apologise for my beginner –  Jan 10 '14 at 16:45
  • In LogCat no, I have to find this in other place? –  Jan 10 '14 at 16:51
  • I 've to study this AsynTask, I try this way, thanks for your help and time Ajeesh –  Jan 10 '14 at 16:55
  • Hi Ajeesh, sorry for my delay, I posted the code, I hope could serve –  Jan 15 '14 at 09:21
  • @UserPax Delete the above comments and I have added the code which worked for me by using port number 8080. I created a server application and started the server in port number 8080 in another machine. – Ajeesh Jan 16 '14 at 05:45
0

You cannot do that, because the emulator does not support ping.

More specifically, it implements a NAT firewall that separates the guest system from the real host network, and which translates ethernet packets (sent to the virtual network adapter) into BSD socket / WinSock library calls

To implement ping properly (i.e. to generate / receive PING datagrams with BSD sockets), a program needs to have root permissions. This is why your 'ping' executable is 'setuid' on Linux.

This also means that there is no way for a 'regular' application like the emulator to do that. Sorry.

Digit
  • 2,073
  • 1
  • 13
  • 10
  • Thanks for your help Digit, so there is no way fot to Ping –  Jan 15 '14 at 09:41
  • Exact, no way to use the ICMP protocol (used by ping). You could still try to connect through TCP or UDP to check if you can do it. This will give you basic connection information (but not the number of hops, latencies, etc.. as reported through ICMP). – Digit Jan 15 '14 at 20:21