1 module client; 2 3 /* 4 * Hunt - A refined core library for D programming language. 5 * 6 * Copyright (C) 2018-2019 HuntLabs 7 * 8 * Website: https://www.huntlabs.net/ 9 * 10 * Licensed under the Apache-2.0 License. 11 * 12 */ 13 14 import std.stdio; 15 16 import hunt.event; 17 import hunt.io.UdpSocket : UdpSocket; 18 19 import std.socket; 20 import std.functional; 21 import std.exception; 22 import core.thread; 23 import core.time; 24 25 // http://www.cs.ubbcluj.ro/~dadi/compnet/labs/lab3/udp-broadcast.html 26 void main() 27 { 28 EventLoop loop = new EventLoop(); 29 30 // UDP Client 31 UdpSocket udpClient = new UdpSocket(loop); 32 33 int count = 3; 34 Address address = new InternetAddress(InternetAddress.ADDR_ANY, InternetAddress.PORT_ANY); 35 udpClient.enableBroadcast(true) 36 .bind(address) 37 // .bind("10.1.222.120", InternetAddress.PORT_ANY) 38 .onReceived((in ubyte[] data, Address addr) { 39 debug writefln("Client => count=%d, server: %s, received: %s", count, 40 addr, cast(string) data); 41 if (--count > 0) 42 { 43 udpClient.sendTo(data, addr); 44 } 45 else 46 { 47 udpClient.sendTo(cast(const(void)[]) "bye!", addr); 48 udpClient.close(); 49 // loop.stop(); 50 } 51 }) 52 .start(); 53 54 udpClient.sendTo(cast(const(void)[]) "Hello world!", parseAddress("255.255.255.255", 8080)); 55 // udpClient.sendTo(cast(const(void)[]) "Hello world!", parseAddress("127.0.0.1", 8090)); 56 // udpClient.sendTo(cast(const(void)[]) "Hello world!", parseAddress("10.1.222.120", 8080)); 57 58 loop.run(); 59 }