April 03, 2014 Re: Sockets between D and C(++) app | ||||
---|---|---|---|---|
| ||||
Posted in reply to FreeSlave | On Wednesday, 2 April 2014 at 21:54:58 UTC, FreeSlave wrote: > It's only server. Maybe problem is on client side. > Yes, it is only a server which needs to answer back the client; And there was the problem: I was not fetching the client's address, and since UDP is an unconnected protocol, I couldn't send it back with just "send". So what I had to do was to create a client address and pass it in receiveFrom(), and then use sendTo(in data, in clientAddr). Once this was fixed, the server was able to answer my client. N.b. auto addr_client = new InternetAddress didn't seem to work, like if InternetAddress wasn't right for the Address parameter. I explicitely needed Address. Here's the code: module main; import std.stdio; import std.socket; import std.string; import std.conv; int main() { auto s = new UdpSocket(); auto addr = new InternetAddress("127.0.0.1", 6666); s.setOption(SocketOptionLevel.IP, SocketOption.REUSEADDR, true); s.bind(addr); Address addr_client = new InternetAddress(6666); while (true) { ubyte[2048] recv_buf; immutable count = s.receiveFrom(recv_buf, addr_client); char[] test = cast(char[])(recv_buf[0..count-1]); // -1 pour compatibilité avec C string... writefln("Received: %s\n", test); auto rep = toStringz("Hello"); s.sendTo(rep[0..6], addr_client); } return 0; } Thanks all for your help, much appreciated! Alexandre |
Copyright © 1999-2021 by the D Language Foundation