let's say we have:
- A is your development machine
- B is your first emulator instance running on A
- C is your second emulator instance running on A
within both B and C, 127.0.0.1 refers to the emulated system's own loopback
interface, but 10.0.2.2 refers to A's loopback interface (a.k.a. A:127.0.0.1).
10.0.2.15 refers to the emulated system's own network address.
let's say we want to run a server on B listening on port 5000
- bind the server in B to udp:10.0.2.15:5000
- setup, on B's console, an udp redirection with: redir add udp:5000:5000
- make the client in C connect to udp:10.0.2.2:5000
- profit !!
this works because the "redir add" creates a redirection from A:
127.0.0.1:5000 to B:10.0.2.15:5000
then C connects to C:10.0.2.2:5000 which is already redirected to A:
127.0.0.1:5000 which goes to B:10.0.2.15:5000
hope this helps
On Tue, Apr 8, 2008 at 8:13 AM, Amit <[EMAIL PROTECTED]> wrote:
>
> Hi all,
>
> I am trying to develop small chat application using UDP in android.
> The problem I am facing here is, I want to run the client on one
> emulator and server on the other.
>
> I am able to run two apps on different emulators, but I am not getting
> the expected output.
>
> Whenever I try to send a packet from client to server, its not getting
> delivered to Server.
>
> Can anybody tell me how to address to another emulator from one
> emulator?
>
> If somehow I can manage to get the IP address of the emulator I think
> it might work.
>
>
> Here is the code:
>
> ServerSide:
>
> package psl.android.udpchatServer;
>
> import java.io.IOException;
> import java.net.DatagramPacket;
> import java.net.DatagramSocket;
> import java.net.InetAddress;
> import java.net.SocketException;
> import java.net.UnknownHostException;
>
> import android.app.Activity;
> import android.os.Bundle;
> import android.util.Log;
> import android.widget.TextView;
>
> public class UDPServer extends Activity {
> TextView tvDisplay;
> /** Called when the activity is first created. */
> @Override
> public void onCreate(Bundle icicle) {
> super.onCreate(icicle);
> setContentView(R.layout.main);
> tvDisplay=(TextView)findViewById(R.id.tvDisplay);
> Thread t=new Thread(new Server(tvDisplay));
> t.start();
> }
> }
>
> class Server implements Runnable{
> public static final String SERVERIP="127.0.0.1";
> public static final int SERVERPORT=4444;
> TextView tvDisplay;
> public Server(TextView display) {
> tvDisplay=display;
> Log.i("server","inside constr");
> }
> @Override
> public void run() {
> // TODO Auto-generated method stub
> try {
>
> InetAddress inetadd=
> InetAddress.getByName(SERVERIP);
> DatagramSocket socket= new
> DatagramSocket(SERVERPORT,inetadd);
> byte buf[]=new byte[3000];
> DatagramPacket packet= new
> DatagramPacket(buf,buf.length);
> Log.i("server","1");
> socket.receive(packet);
> Log.i("server","2");
> Log.i("server",new String(packet.getData()));
> tvDisplay.setText(new String(packet.getData()));
> buf="Packet Received".getBytes();
> packet=new DatagramPacket(buf,buf.length);
> Log.i("server","3");
> socket.send(packet);
> Log.i("server","4");
> } catch (UnknownHostException e) {
> // TODO Auto-generated catch block
> e.printStackTrace();
> }catch (SocketException e) {
> // TODO Auto-generated catch block
> e.printStackTrace();
> }catch (IOException e) {
> // TODO Auto-generated catch block
> e.printStackTrace();
> }
>
> }
>
> }
>
> ClientSide:
>
> package psl.android.udpchatClient;
>
> import java.io.IOException;
> import java.net.DatagramPacket;
> import java.net.DatagramSocket;
> import java.net.InetAddress;
> import java.net.SocketException;
> import java.net.UnknownHostException;
>
>
> import com.google.android.tests.core.RegexTest.Find;
>
> import android.app.Activity;
> import android.os.Bundle;
> import android.util.Log;
> import android.widget.TextView;
>
> public class UDPClient extends Activity {
> TextView tvDisplay;
> /** Called when the activity is first created. */
> @Override
> public void onCreate(Bundle icicle) {
> super.onCreate(icicle);
> setContentView(R.layout.main);
> tvDisplay=(TextView)findViewById(R.id.tvDisplay);
> Thread t=new Thread(new Client(tvDisplay));
> t.start();
> }
> }
>
> class Client implements Runnable{
> public static final String SERVERIP="127.0.0.1";
> public static final int SERVERPORT=4444;
> TextView tvDisplay;
> public Client(TextView display) {
> tvDisplay=display;
> Log.i("client","inside constr");
> }
> @Override
> public void run() {
> // TODO Auto-generated method stub
> try {
> InetAddress
> iadd=InetAddress.getByName(SERVERIP);
> DatagramSocket socket=new DatagramSocket();
> byte[] buff="Hello Server".getBytes();
> DatagramPacket packet=new
> DatagramPacket(buff,buff.length,iadd,SERVERPORT);
> Log.i("client","1");
> socket.send(packet);
> Log.i("client","2");
> buff=new byte[3000];
> packet=new
> DatagramPacket(buff,buff.length);
> Log.i("client","3");
> socket.receive(packet);
> Log.i("client","4");
> Log.i("client",new
> String(packet.getData()));
> tvDisplay.setText(new
> String(packet.getData()));
> } catch (UnknownHostException e) {
> // TODO Auto-generated catch block
> e.printStackTrace();
> }catch (SocketException e) {
> // TODO Auto-generated catch block
> e.printStackTrace();
> } catch (IOException e) {
> // TODO Auto-generated catch block
> e.printStackTrace();
> }
> }
>
> }
>
> In the above code, Server runs until the point Log.i("server","1");
> and waits for packet.
> and when I run the client on another emulator it runs upto
> Log.i("client","2");
> That means client is sending the packet but somehow its not reaching
> to another emulator where server is running.
> >
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[EMAIL PROTECTED]
Announcing the new M5 SDK!
http://android-developers.blogspot.com/2008/02/android-sdk-m5-rc14-now-available.html
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---