Skip to content

Instantly share code, notes, and snippets.

@itzmeanjan
Created April 29, 2019 09:04
Show Gist options
  • Save itzmeanjan/7288e0e9e1be4ae333a4f2761d156ffd to your computer and use it in GitHub Desktop.
Save itzmeanjan/7288e0e9e1be4ae333a4f2761d156ffd to your computer and use it in GitHub Desktop.
import 'dart:io'
show RawDatagramSocket, RawSocketEvent, InternetAddress, Datagram;
import 'dart:convert' show utf8;
main() =>
// UDP server
RawDatagramSocket.bind(InternetAddress.anyIPv4, 8000)
.then((datagramSocket) {
datagramSocket.readEventsEnabled = true;
datagramSocket.listen((RawSocketEvent event) {
if (event == RawSocketEvent.read) {
Datagram dg = datagramSocket.receive();
if (dg != null) {
datagramSocket.send(dg.data, dg.address, dg.port);
print('${dg.address}:${dg.port} -- ${utf8.decode(dg.data)}');
}
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment