Forum Navigation
UDP socket with Tango
Posted: 04/17/08 18:17:21Hi,
I'm trying to create a bare bones UDP program that sends and receives data on a given local port. I wrote a short program, which compiles, runs and returns data, but instead of connecting and sending/receiving it does something else and continues without error whether or not there's something to listen to or not. To prove I'm not going crazy I implememted a very similar C# program running as I wished and that program works fine. Do you have an idea what I can do to make this piece of code work?
private import tango.io.Console; private import tango.net.Socket, tango.net.InternetAddress; private import tango.io.Stdout; void main() { auto socket = new Socket(AddressFamily.INET, SocketType.DGRAM, ProtocolType.UDP); socket.blocking = false; auto localEndpoint = new InternetAddress ("localhost", 1979); socket.bind(localEndpoint); byte[8] cmd; cmd[0] = cast(byte)'a'; cmd[1] = cast(byte)'b'; cmd[2] = cast(byte)'c'; cmd[3] = cast(byte)'d'; cmd[4] = cast(byte)'e'; cmd[5] = cast(byte)'f'; cmd[6] = cast(byte)'g'; cmd[7] = cast(byte)'h'; byte[] buf = new byte[cmd.length]; int res = socket.sendTo(buf, new InternetAddress ("localhost", 1979)); byte[] inBuf = new byte[1026]; while (1) { int res2 = socket.receiveFrom(inBuf, new InternetAddress ("localhost", 1979)); Stdout (res); } }