I created a toy example for the echo client and UDP server. However, I am not receiving a response from the server, and I am wondering what I am doing wrong.
Customer:
#!/usr/bin.rdmd import std.stdio; import std.socket; import std.string; import std.conv; import std.random; import std.outbuffer; int main(string[] args) { if (args.length != 3) { writefln("usage: %s <server host> <port>",args[0]); return 0; } auto s = new UdpSocket(); auto addr = new InternetAddress(args[1], to!ushort(args[2])); s.connect(addr); scope(exit) s.close(); for (int i = 0; i < 1000; i++){ auto r = uniform(int.min,int.max); auto send_buf = new OutBuffer(); send_buf.write(r); s.send(send_buf.toBytes()); ubyte[r.sizeof] recv_buf; s.receive(recv_buf); assert(r == *cast(int*)(send_buf.toBytes().ptr)); } return 0; }
Server:
#!/usr/bin.rdmd import std.stdio; import std.socket; import std.string; import std.conv; int main(string[] args) { if (args.length != 2) { writefln("usage: %s <port>",args[0]); return 0; } auto s = new UdpSocket(); auto addr = new InternetAddress("localhost", to!ushort(args[1])); s.bind(addr); while (true){ ubyte[int.sizeof] recv_buf; s.receive(recv_buf); writefln("Received: %s\n",recv_buf); s.send(recv_buf); } writeln("sent"); return 0; }
If you run programs, you will see that the client hangs upon reception, and the server has already sent a response.
Do you know what I am doing wrong?
BTW, What is the best resource for network programming in D?
source share