www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Interprocess Communication between Python and D

reply Utk <bhiogade20 gmail.com> writes:
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?
Jun 23 2021
next sibling parent reply jfondren <julian.fondren gmail.com> writes:
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]
Jun 23 2021
next sibling parent jfondren <julian.fondren gmail.com> writes:
On Thursday, 24 June 2021 at 05:53:51 UTC, jfondren wrote:
 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]
Some alternatives: Server said: \0f\0c\00\00\00\01\00\00\00\00\00\01\00\00\00\00\00\00\00\00\04\00\00\00\00\00\00\00 from: ```d ubyte[1024] buffer; ... writefln("Server said: %(\\%02x%)", buffer[0 .. received]); ``` Server said: "\x0F\f\0\0\0\x01\0\0\0\0\0\x01\0\0\0\0\0\0\0\0\x04\0\0\0\0\0\0\0" from: ```d char[1024] buffer; ... writefln("Server said: %(%s%)", [buffer[0 .. received]]); ```
Jun 23 2021
prev sibling parent reply Utk <bhiogade20 gmail.com> writes:
On Thursday, 24 June 2021 at 05:53:51 UTC, jfondren wrote:
 On Thursday, 24 June 2021 at 04:28:45 UTC, Utk wrote:
 But all of that's working just fine. Change the buffer to 
 ubyte[1024]
Oh, I see! Thanks a lot, will try that.
Jun 23 2021
parent reply Nicholas Wilson <iamthewilsonator hotmail.com> writes:
On Thursday, 24 June 2021 at 06:10:08 UTC, Utk wrote:
 Oh, I see! Thanks a lot, will try that.
For future questions please use the learn forum: https://forum.dlang.org/group/learn
Jun 24 2021
parent Utk <bhiogade20 gmail.com> writes:
On Thursday, 24 June 2021 at 07:56:35 UTC, Nicholas Wilson wrote:
 On Thursday, 24 June 2021 at 06:10:08 UTC, Utk wrote:
 Oh, I see! Thanks a lot, will try that.
For future questions please use the learn forum: https://forum.dlang.org/group/learn
Sure!
Jun 24 2021
prev sibling parent reply Adam D Ruppe <destructionator gmail.com> writes:
On Thursday, 24 June 2021 at 04:28:45 UTC, Utk wrote:
 (same code as mentioned in the above link)
That code was assuming you were sending strings which is why it did that. I wrote a newer version of that socket tutorial btw here: http://dpldocs.info/this-week-in-d/Blog.Posted_2019_11_11.html But I again used `char` there. However, if you just change that to `ubyte`, you can use the rest of the code for binary data. (technically, the code works the same way, it is just the `write` that formats it differently for char vs ubyte when it is displayed, but still ubyte is generally the right thing to do anyway, i probably should have used it even if i am sending hello strings.)
Jun 24 2021
parent Utk <bhiogade20 gmail.com> writes:
On Thursday, 24 June 2021 at 12:06:30 UTC, Adam D Ruppe wrote:
 On Thursday, 24 June 2021 at 04:28:45 UTC, Utk wrote:
 (same code as mentioned in the above link)
That code was assuming you were sending strings which is why it did that. I wrote a newer version of that socket tutorial btw here: http://dpldocs.info/this-week-in-d/Blog.Posted_2019_11_11.html But I again used `char` there. However, if you just change that to `ubyte`, you can use the rest of the code for binary data. (technically, the code works the same way, it is just the `write` that formats it differently for char vs ubyte when it is displayed, but still ubyte is generally the right thing to do anyway, i probably should have used it even if i am sending hello strings.)
I see! Thanks a lot for the information and your code snippets, they helped a lot!
Jun 24 2021