0

I'm trying to do a very simple client on android to send an UDP message to a server on Android.

Here's my simple UDP client:

public class MyUDPClient {
    private static String TAG = "MyUDPClient";
    private DatagramSocket udpSocket;
    private InetAddress serverAddress;
    private int port;
    private Scanner scanner;

    public MyUDPClient(String destinationAddr, int port) throws SocketException, UnknownHostException {
        this.serverAddress = InetAddress.getByName(destinationAddr);
        this.port = port;
        udpSocket = new DatagramSocket(this.port);
        scanner = new Scanner(System.in);
    }

    public void send(String message) throws IOException {
        DatagramPacket p = new DatagramPacket(
                message.getBytes(), message.getBytes().length, serverAddress, port);
        Log.d(TAG, "udp client send: " + message + " to serverAddress " + serverAddress);
        this.udpSocket.send(p);
    }

    public void connect() {
        Log.d(TAG, "UDP connection to port " + port);
    }
}

And here's my simple UDP server:

public class MyUDPServer  {
    private String TAG = "MyUDPServer";
    private int port;
    private DatagramSocket udpSocket;
    OnMessageCallback onMessageCallback;

    public MyUDPServer(int port) throws SocketException {
        this.udpSocket = new DatagramSocket(port);
        this.port = port;
    }


    public interface OnMessageCallback {
        public void on(String message);
    }

    private void listen() throws Exception {
        Log.d(TAG, "listening UDP server on port " + port);
        String msg;

        while (true) {
            byte[] buf = new byte[256];
            DatagramPacket packet = new DatagramPacket(buf, buf.length);

            // blocks until a packet is received
            udpSocket.receive(packet);
            msg = new String(packet.getData()).trim();
            Log.d(TAG, "message received; " + msg);
            onMessageCallback.on(msg);
        }
    }

    public void start() throws Exception {
        listen();
    }

    public void setMessageCallback(OnMessageCallback onMessageCallback) {
        this.onMessageCallback = onMessageCallback;
    }

}

The problem is that the server never receives any messages. I start the client like this:

MyUDPClient myUDPClient = new MyUDPClient("192.168.1.6", 8887);

Since it's on an emulator, I try to filter the IP 192.168.1.6 and even though I call myUDPClient.send("message"), on WireShark I receive nothing, which indicates that the message is not even leaving the computer where the emulator tuns.

What am I doing wrong?

I've created a WebSocket client/server on Android and it worked fine.

I have these 2 permissions:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Paprika
  • 402
  • 5
  • 18
  • 1
    The emulator is not on the same network as your Android app phone. So unreachable. – blackapps Nov 04 '20 at 22:31
  • Further you did not tell if the client or server was running on an emulator. Not that it matters.. – blackapps Nov 04 '20 at 22:32
  • @blackapps the client is on the emulator. But the websocket worked in the emulator and reached a phone in the same network – Paprika Nov 04 '20 at 22:37
  • @blackapps I just used a phone instead of an emulator and the same problem happens, I see nothing on the server, which is in the same network – Paprika Nov 04 '20 at 22:43
  • You used two phones now? Is that what you said? You did not tell where your server runs in your last comment. – blackapps Nov 04 '20 at 22:50
  • @blackapps first test: client on emulator, server on phone 192.168.1.6. Second test: client on phone, server on phone 192.168.1.6. If the problem was that the phone was unreachable then I'd still see the output in Wireshark in the first test with emulator – Paprika Nov 04 '20 at 22:55
  • How do you start the server? And why is the client sending to its own port number instead of the server's? – user207421 Nov 04 '20 at 23:24
  • @MarquisofLorne I start the server simply as `myServer = new MyUDPServer(port);` – Paprika Nov 05 '20 at 03:13
  • @MarquisofLorne the client uses the same port as the server – Paprika Nov 05 '20 at 03:13
  • Not if they're both in the same host it doesn't, not with this code. Duplicate of [`NetworkOnMainThreadException`](https://stackoverflow.com/questions/6343166/how-to-fix-android-os-networkonmainthreadexception). – user207421 Nov 05 '20 at 04:09

2 Answers2

0

In your MyUDPClient-class simply change to avoid a "double" binding that causes the exception:

public MyUDPClient(String destinationAddr, int port) throws SocketException, UnknownHostException {
  super();
  this.serverAddress = InetAddress.getByName(destinationAddr);
  this.port = port;
  udpSocket = new DatagramSocket(this.port);
  scanner = new Scanner(System.in);
}

to

public MyUDPClient(String destinationAddr, int port) throws SocketException, UnknownHostException {
  super();
  this.serverAddress = InetAddress.getByName(destinationAddr);
  this.port = port;
  udpSocket = new DatagramSocket();
  //udpSocket = new DatagramSocket(this.port);
  scanner = new Scanner(System.in);
}
Michael Fehr
  • 5,827
  • 2
  • 19
  • 40
0

Problem was that I was gatting NetworkOnMainThreadException but it was being logged to another part of the logcat that I wasn't filtering for.

Doing the myudpClient.send("myMessage") from a separate thread solved the problem. That's why I couldn't even see the output on Wireshark, it wasn't even sending.

user207421
  • 305,947
  • 44
  • 307
  • 483
Paprika
  • 402
  • 5
  • 18