Sockets between D and C(++) app
Alexandre L.
alex.cs00 at yahoo.ca
Thu Apr 3 05:25:44 PDT 2014
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
More information about the Digitalmars-d-learn
mailing list