Interprocess Communication between Python and D

jfondren julian.fondren at gmail.com
Thu Jun 24 05:53:51 UTC 2021


On Thursday, 24 June 2021 at 04:28:45 UTC, Utk wrote:
> I'm trying to send data from python script to D using tcp 
> sockets. As I'm new to D I referred to 
> https://forum.dlang.org/post/lqahvaeqddaddnkhpfyf@forum.dlang.org for client and server code. When I'm sending packed data from python it is of the form
> ```d
> u'\x0f\x10\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00\x00\x00\x04\x00\x00\x00\x00\x00\x00\x00\xbaH\x0c\x00'
> ```
> basically a string! This data is successfully received on the 
> client side in D but when the data is changing to
> ```d
> u'\x0f\x0c\x00\x00\x00\x01\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00\x00\x00\x00\x00\x00\x00'
> ```
> I'm receiving nothing on the client-side in D.
> Client code:
> ```d
> void main() {
>     import std.socket, std.stdio;
>     auto socket = new Socket(AddressFamily.INET,  
> SocketType.STREAM);
>     char[1024] buffer;
>     socket.connect(new InternetAddress("127.0.0.1", 10000));
>     auto received = socket.receive(buffer); // wait for the 
> server to say hello
>     writeln("Server said: ", buffer[0 .. received]);
> }
> ```
> (same code as mentioned in the above link)
> **Can someone please help me with this?**
> Also when I'm sending the data in the 1st format, I'm receiving 
> ```�H``` as an output on the client, what changes in code 
> should I make so that I receive the data in the same format as 
> sent from Python?

As the buffer's a char[], it's being printed as a string, which
means dumping those raw NUL bytes and such into the terminal, 
which
is displaying them in a way that's surprising you.

But all of that's working just fine. Change the buffer to 
ubyte[1024]
and with no other changes you'll get this output:

Server said: [15, 16, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
4, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 186, 72, 12, 0]


More information about the Digitalmars-d mailing list