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
-~----------~----~----~----~------~----~------~--~---