Hi Amit!

I am also facing a similar problem. And this is happening in my case
because, the IP address are getting resolved to 127.0.0.1, which is
the emulator IP itself. Infact, I have posted this problem in the
forum, as to how can we obtain, interface IP Addresses. Once we have
the interface IP address something like 192.168.1.2, then the next
step that would want to do is to write a small packet relay and make
both server and client communicate with this relay. In response to the
relay can send the IP address and port from which the packet was
received.

This is needed since, Android is in an emulated mode, it will do some
sort of internal port mapping before the packet is sent out. And
unless you send the packet out of each of the emulators it will not be
possible for the emulator to route the packet in. So once you have
done that, you can send packets directly to ports which have been
allocated by the emulators. This solution is what we usually use in
NAT traveral, a minimalistic version of STUN protocol.

Regards
Nitin Khanna
Hughes Systique Corporation

On Apr 7, 11:13 pm, 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 android-developers@googlegroups.com
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to