softwaresupply
softwaresupply

Reputation: 1948

Cannot receive data by a socket from a device (in WLAN) via UDP

my android device is connected to my home-wireless-network. Also a special UDP-device is connected to it. My android app successfully can send commands to the UDP-device. But if I open a socket it does not receive data. Can you see what is wrong? I know the ip of the UDP-device from the iphone-APP which is working

Here is how the app send commands:

public static final String SERVERIP = "192.168.2.114";
public static final int PORT = 44444;
public void run() {
    try {
        serverAddr = InetAddress.getByName(SERVERIP);
        DatagramSocket socket = new DatagramSocket();
        byte[] buf = message.toByteArray();
        DatagramPacket packet = new DatagramPacket(buf, buf.length,     serverAddr, PORT);
        socket.send(packet);
        socket.close();
    } catch (Exception e) {
        Log.e("UDP", "Sender/Client: Error", e);
    }
}

Whereas I have two approaches for receiving data:

public static final String SERVERIP = "192.168.2.114";
public static final int SERVERPORT = 44445;

private InetAddress serverAddr;

public void run() {
    try {
        InetAddress serverAddr = InetAddress.getByName(SERVERIP);
        DatagramSocket socket = new DatagramSocket(SERVERPORT, serverAddr);
        byte[] buf = new byte[65213];
        DatagramPacket packet = new DatagramPacket(buf, buf.length);
        socket.receive(packet);
    } catch (Exception e) {
        Log.e("UDP", "Receiver: Error", e);
    }

    try {
        serverAddr = InetAddress.getByName(SERVERIP);
        DatagramChannel channel = DatagramChannel.open();
        DatagramSocket socket = channel.socket();
        byte[] buf = new byte[65213];
        DatagramPacket packet = new DatagramPacket(buf, buf.length,     serverAddr, SERVERPORT);
        socket.receive(packet);
        socket.close();
    } catch (Exception e) {
        Log.e("UDP", "Sender/Client: Error", e);
    }
}

The approach in the first try block leads to an exception:

java.net.BindException: Cannot assign requested address
at org.apache.harmony.luni.platform.OSNetworkSystem.bind(Native Method)
at dalvik.system.BlockGuard$WrappedNetworkSystem.bind(BlockGuard.java:268)
at org.apache.harmony.luni.net.PlainDatagramSocketImpl.bind(PlainDatagramSocketImpl.java:81)
at java.net.DatagramSocket.createSocket(DatagramSocket.java:193)
at java.net.DatagramSocket.<init>(DatagramSocket.java:95)
at de.myappname.connection.Receiver.run(Receiver.java:29)
at java.lang.Thread.run(Thread.java:1019)

The second approach just blocks the thread by socket.receive(packet) which does not receive data. From the iphone and specification I know the device sends data via UDP 44445 over WLAN. Any suggestions what is wrong? Thank you!

Upvotes: 0

Views: 1563

Answers (2)

Peter Knego
Peter Knego

Reputation: 80340

UDP port 44445 is used by eMule protocol. Do you have any other eMule clients active on your device?

Update:

The problem seems to be the address you bind to - it must be an address on the localhost, i.e. IP address of your device, not remote device. See DatagramSocket(port, InetAddress) constructor.

Upvotes: 0

ecle
ecle

Reputation: 4000

I guess you need to put the receive() function inside a while loop since you current code looks like it receives message only once; it doesn't guarantee that it will contain any valid data.

Upvotes: 0

Related Questions