2

The server is on the PC:

sendData = "server msg here".getBytes();

DatagramPacket sendPacket = new DatagramPacket(sendData,
        sendData.length, ipAddr, portNb);
try {
    sendSock.send(sendPacket);
    Thread.sleep(1000);
    System.out.println("sent msg");

} catch (IOException e) {
    e.printStackTrace();
} catch (InterruptedException e) {
    e.printStackTrace();
}

The fields ipAddr and portNb ARE NOT null. In the android application a thread runs:

sendSocket = new DatagramSocket();

sendSocket.setSoTimeout(3000);
sendSocket.setReuseAddress(true);

//...

try {
    receivePacket = new DatagramPacket(receiveData, receiveData.length);
    serverSocket.receive(receivePacket);
    System.out.println("droid now is "
            + new String(receivePacket.getData()));

} catch (IOException e) {
    e.printStackTrace();
}

I use the 57111 port on both sides and I have redirected it on the Android emulator on the same port.

In the Android I have hard-coded the IP address of my pc, and on the server side I tried 10.0.2.2, 10.0.2.15 and also the IP address obtained by using the method of the second answer to this question.

I have set a timeout on the Android side for the socket and the trace is :

`02-27 23:12:57.907: W/System.err(13993): java.net.SocketTimeoutException: Try again
02-27 23:12:57.907: W/System.err(13993):    at  org.apache.harmony.luni.platform.OSNetworkSystem.recv(Native Method)
02-27 23:12:57.917: W/System.err(13993):    at dalvik.system.BlockGuard$WrappedNetworkSystem.recv(BlockGuard.java:321)
02-27 23:12:57.927: W/System.err(13993):    at org.apache.harmony.luni.net.PlainDatagramSocketImpl.doRecv(PlainDatagramSocketImpl.java:172)
02-27 23:12:57.927: W/System.err(13993):    at org.apache.harmony.luni.net.PlainDatagramSocketImpl.receive(PlainDatagramSocketImpl.java:181)
02-27 23:12:57.927: W/System.err(13993):    at java.net.DatagramSocket.receive(DatagramSocket.java:402)
02-27 23:12:57.938: W/System.err(13993):    at com.example.testinger.ReceiveThread.run(ReceiveThread.java:39)
02-27 23:12:58.987: W/KeyCharacterMap(13993): No keyboard for id 0
02-27 23:12:58.987: W/KeyCharacterMap(13993): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
02-27 23:12:59.377: W/IInputConnectionWrapper(13993): showStatusIcon on inactive InputConnection
02-27 23:13:00.947: W/System.err(13993): java.net.SocketTimeoutException: Try again
02-27 23:13:01.057: W/System.err(13993):    at org.apache.harmony.luni.platform.OSNetworkSystem.recv(Native Method) `

I also tried it on an actual phone and the results are the same. On the Android side the messages do not arrive, the receive method waits until it times out. It has the permissions

  • android.permission.INTERNET
  • android.permission.ACCESS_NETWORK_STATE
  • android.permission.ACCESS_WIFI_STATE
  • android.permission.CHANGE_WIFI_MULTICAST_STATE

UPDATE: I tried a simple send-receive program and that didn't work either:the client waits for a packet from the server before being displayed.

Community
  • 1
  • 1
  • So the emulator uses 10.0.2.2 as the server address? Moreover, for the real Android device, the IP of the server is usually something like 192.168.*.* - so you will need different configurations for the emulator and your device. Did you take this into account? – cyroxx Feb 27 '13 at 23:41
  • I am not sure of the emulator's ip that may be the problem, but since I hard coded every ip I could think of into the server at the pc I don't think that's the problem. – Phantasme Punk Feb 27 '13 at 23:45
  • Well, [according to the docs](http://developer.android.com/tools/devices/emulator.html#emulatornetworking) the IP of emulator should be 10.0.2.15, but obviously you already tried that. Maybe a stupid question, but I assume both the server and the emulator run on the same machine? – cyroxx Feb 28 '13 at 00:16
  • Yes they are on the same pc. – Phantasme Punk Feb 28 '13 at 00:23

1 Answers1

0

It seems like your server or port is not reachable from the device. Are you running any HTTP Server on your machine, you could use the browser to test the same.

Alternatively you can use code - Refer to below- Android: How to check if the server is available? How to test if a remote system is reachable

Community
  • 1
  • 1
Ashwini Bhangi
  • 291
  • 1
  • 6
  • I can and did send messages to the server, the server firstly waits for a message from the Android application, it's the ip on the emulator that I'm not sure of. – Phantasme Punk Feb 27 '13 at 22:08
  • Hi, can you explain it clearly how the flow is happening right now ? Who initiates the sequence and if packets sent by the client are received on the server or vice versa. May be just writing it step by step will help. Thanks – Ashwini Bhangi Feb 28 '13 at 10:19
  • 1. datagram socket on on pc server waits on port 57111 for datagram packet through the receive method – Phantasme Punk Feb 28 '13 at 12:18
  • All that I could really use is a basic COMPLETE demo to send a message from pc to android on udp 57111 (or whatever number port) and display the message in the android application, I searched thoroughly on google and didn't find, I can upload the application somewhere but it's gonna take a while. SO a send from pc receive and display on android is all I'm asking for – Phantasme Punk Feb 28 '13 at 12:33
  • I would recommend going to basics - are you able to ping the android client on the ip & port via command line ? You can try it from any machine that's on the same network – Ashwini Bhangi Feb 28 '13 at 13:17
  • I searched for that also but it says on the android.developer site that it doesn't support ping. – Phantasme Punk Feb 28 '13 at 14:21