Re: Sockets between D and C(++) app

2014-04-03 Thread Alexandre L.

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, );
s.setOption(SocketOptionLevel.IP, SocketOption.REUSEADDR, true);
s.bind(addr);
Address addr_client = new InternetAddress();

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


Re: Sockets between D and C(++) app

2014-04-02 Thread FreeSlave

It's only server. Maybe problem is on client side.

Try this if you are on Linux:

//Linux C client
#include sys/socket.h
#include sys/types.h
#include netinet/in.h
#include arpa/inet.h
#include stddef.h
#include string.h
#include stdio.h

int main()
{
int sock, res;
struct sockaddr_in addr;
const char* hello;
size_t len;

sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock  0)
{
printf(Can't create socket\n);
return -1;
}

addr.sin_family = AF_INET;
addr.sin_port = htons(5432);
addr.sin_addr.s_addr = inet_addr(127.0.0.1);

hello = Hello from C;
len = strlen(hello);
while (1)
{
res = sendto(sock, hello, len, 0, (struct 
sockaddr*)addr, sizeof(addr));

if (res = 0)
{
printf(Can't send\n);
return -1;
}
}
close(sock);
return 0;
}


//D server
import std.socket;
import std.stdio;

int main(string[] args)
{
auto s = new UdpSocket(AddressFamily.INET);

auto addr = new InternetAddress(127.0.0.1, 5432);
s.setOption(SocketOptionLevel.IP, SocketOption.REUSEADDR, 
true);

s.bind(addr);

while (true)
{
ubyte[2048] recv_buf;
int count = s.receiveFrom(recv_buf[]);
char[] test = cast(char[])recv_buf;

writefln(Received: %s\n, test);
}
return 0;
}


Note that you don't need to remove zero-symbol if you don't pass 
it from client.


Re: Sockets between D and C(++) app

2014-04-01 Thread bearophile

Alexandre L.:

Some comments on your code:


Here's my 'server' code:

int main(string[] args)
{


If you don't need args, then I suggest to not put it as main 
argument. So probably this is better (note no int nor return, in 
D they are not needed):


void main() {
...
}



int count = s.receiveFrom(recv_buf);


It's better to use immutable here, and infer the type (use 
const/immutable everywhere you don't really need to mutate a 
variable/argument/field):


immutable count = s.receiveFrom(recv_buf);


		char[] test = cast(char[])recv_buf[0..count-1]; // -1 for C 
string comp.


writefln(Received: %s\n, test);

char[] rep = regan\0.dup;

s.send(cast(ubyte[])rep);


casts are dangerous, because they silently assume you know what 
you are doing. As first try I suggest you to remove every cast() 
from your D program, and replace them with to!() or other 
functions like toStringz. Sometimes this is not the most 
efficient thing to do, but it's safer, so it's better when you 
start to learn D.


Bye,
bearophile


Re: Sockets between D and C(++) app

2014-04-01 Thread Alexandre L.

On Wednesday, 2 April 2014 at 00:34:08 UTC, bearophile wrote:

char[] rep = regan\0.dup;

s.send(cast(ubyte[])rep);


casts are dangerous, because they silently assume you know what 
you are doing. As first try I suggest you to remove every 
cast() from your D program, and replace them with to!() or 
other functions like toStringz. Sometimes this is not the most 
efficient thing to do, but it's safer, so it's better when you 
start to learn D.


Bye,
bearophile


Thanks for your reply. As for cast, I seems not to have any 
option, because to! doesn't work as I would expect it. It would 
return me an array of numbers, instead of a string; So I kept the 
cast here, since I certainly know what it's doing -for now-.


I generally use immutables, you caught me, here :-). As of main 
and return, I was not aware we could just ignore them if we 
didn't need them. I love explicit programming.


However, I'm still stuck with toStringz(). Since it returns an 
immutable(char[]), I can't find how to pass it to Socket.send(), 
and I do not seem to be able to cast away the immutable :-s


Alexandre


Re: Sockets between D and C(++) app

2014-04-01 Thread Alexandre L.

My bad; It returns immutable(char)*.

Still won't work with send(); Am I right to supposed the 
receiving client must handle a ubyte[] as well (C++) ?


Re: Sockets and D?

2009-04-05 Thread downs
Jimi_Hendrix wrote:
 Hi, I am new to D but not to programming.  I have had some socket experience 
 before.  How would i connect to a server using sockets in D?  A link to a D 
 socket tutorial (if one exists) would also be appreciated.
 
 by the way, first post to a newsgroup for me

As certain Tango users have neglected to tell you, Phobos also has a perfectly 
servicable socket interface in std.socket. If you know Sockets in C, you should 
have no problem with it.

http://digitalmars.com/d/1.0/phobos/std_socket.html


Re: Sockets and D?

2009-04-02 Thread Denis Koroskin

On Fri, 03 Apr 2009 04:19:10 +0400, Jimi_Hendrix myspo...@gmail.com wrote:

Hi, I am new to D but not to programming.  I have had some socket  
experience before.  How would i connect to a server using sockets in D?   
A link to a D socket tutorial (if one exists) would also be appreciated.


by the way, first post to a newsgroup for me


Hi!

I'd suggest you to look at Tango as it has very impressive networking feature 
set.
Alternatively, you can use all those BSD sockets API functions from D directly 
(as you'd do in C/C++), but I wouldn't recommend going that low-lever.



Re: Sockets and D?

2009-04-02 Thread jimi hendrix
On Fri, 03 Apr 2009 04:35:21 +0400, Denis Koroskin wrote:

 I'd suggest you to look at Tango as it has very impressive networking
 feature set.

tutorial? also what do i need to download to use tango?


Re: Sockets and D?

2009-04-02 Thread Denis Koroskin

On Fri, 03 Apr 2009 04:55:59 +0400, jimi hendrix myspo...@gmail.com wrote:


On Fri, 03 Apr 2009 04:35:21 +0400, Denis Koroskin wrote:


I'd suggest you to look at Tango as it has very impressive networking
feature set.


tutorial? also what do i need to download to use tango?


http://dsource.org/projects/tango
http://dsource.org/projects/tango/wiki/Download
http://dsource.org/projects/tango/wiki/Tutorials
http://dsource.org/projects/tango/wiki/Examples

Tango usually bundled with a compiler so that you get started faster.