AndroidAsync icon indicating copy to clipboard operation
AndroidAsync copied to clipboard

Can we Listen to a UDP broadcast from a server with AndroidAsync

Open mhussainkhan opened this issue 8 years ago • 1 comments

I cant receive msg from server to client..

Server Side:

` public class Server {

private InetSocketAddress host;
private AsyncDatagramSocket asyncDatagramSocket;
Context context;
TextView tv;
public Server(String host, int port,Context conetxt) {
    this.host = new InetSocketAddress(host, port);
    context=conetxt;
    setup();

}

private void setup() {

    try {
        asyncDatagramSocket = AsyncServer.getDefault().openDatagram(host, true);
    } catch (IOException e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    }

    asyncDatagramSocket.setDataCallback(new DataCallback() {
        @Override
        public void onDataAvailable(DataEmitter emitter, ByteBufferList bb) {
            System.out.println("[Server] Received Message " + new String(bb.getAllByteArray()));

        }
    });

    asyncDatagramSocket.setClosedCallback(new CompletedCallback() {
        @Override
        public void onCompleted(Exception ex) {
            if (ex != null) throw new RuntimeException(ex);
            System.out.println("[Server] Successfully closed connection");
        }
    });

    asyncDatagramSocket.setEndCallback(new CompletedCallback() {
        @Override
        public void onCompleted(Exception ex) {
            if (ex != null) throw new RuntimeException(ex);
            System.out.println("[Server] Successfully end connection");
        }
    });
}


public void send(String msg) {
    asyncDatagramSocket.send(host, ByteBuffer.wrap(msg.getBytes()));
}

} Client Side: public class Client {

private final InetSocketAddress host;
private AsyncDatagramSocket asyncDatagramSocket;

public Client(String host, int port) {
    this.host = new InetSocketAddress(host, port);
    setup();
}

private void setup() {
    try {
        asyncDatagramSocket = AsyncServer.getDefault().connectDatagram(host);
    } catch (IOException e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    }

    asyncDatagramSocket.setDataCallback(new DataCallback() {
        @Override
        public void onDataAvailable(DataEmitter emitter, ByteBufferList bb) {
            System.out.println("[Client] Received Message " + new String(bb.getAllByteArray()));
        }
    });

    asyncDatagramSocket.setClosedCallback(new CompletedCallback() {
        @Override
        public void onCompleted(Exception ex) {
            if(ex != null) throw new RuntimeException(ex);
            System.out.println("[Client] Successfully closed connection");
        }
    });

    asyncDatagramSocket.setEndCallback(new CompletedCallback() {
        @Override
        public void onCompleted(Exception ex) {
            if(ex != null) throw new RuntimeException(ex);
            System.out.println("[Client] Successfully end connection");
        }
    });
}

public void send(String msg) {
    asyncDatagramSocket.send(host, ByteBuffer.wrap(msg.getBytes()));
}

}`

mhussainkhan avatar Apr 19 '17 21:04 mhussainkhan

@mhussainkhan where you able to solve this issue .... i have a similar problem

kolanse avatar Dec 01 '21 22:12 kolanse