[drupal-devel] OT: Binary data.
Hello, I know that this may be a little off topic, but I know most people here deal with php most of the time. I need to write a little test suite, and the only thing that is stopping this from being easy is that I need to know how to write a raw integer (4 binary chars) into a socket. I can do this with c, it is just... int c; write(sockfd, c, sizeof(int)); So what would this be in php. I was wondering if someone can shed some light on it. Thanks in advance. -- Gordon Heydon <gordon@heydon.com.au>
Hello, I wish it was, but this is a custom stream to talk to a PICK database to extract information for the web. The problem that the protocol is proprietary crap which has one binary int field at the beginning of the request, and the rest is text based. Thanks. Gordon. On Tue, 2005-03-15 at 13:43 +0100, Karoly Negyesi wrote:
int c; write(sockfd, c, sizeof(int));
So what would this be in php.
Have no idea. But there is a great library called Drupal :D which has an even greater drupal_http_request which deals with stuff like this.
Regards
ChX
!DSPAM:4236d8d268691490819328!
-- Gordon Heydon <gordon@heydon.com.au>
Gordon Heydon wrote:
Hello,
I wish it was, but this is a custom stream to talk to a PICK database to extract information for the web.
The problem that the protocol is proprietary crap which has one binary int field at the beginning of the request, and the rest is text based.
Thanks. Gordon.
On Tue, 2005-03-15 at 13:43 +0100, Karoly Negyesi wrote:
int c; write(sockfd, c, sizeof(int));
So what would this be in php.
You do realize that the format of the integer "c" might well be different on the writing system and on the receiving system, don't you? Big endian versus little endian, or 2's-complement versus 1's-complment, for example. This is why RPC libraries always define external data formats (XDF), so that every process agrees on the same 4-byte representation of an integer. Having said that, take a look at the socket_send() page in the user comments. I think you will have to do your own conversion of the integer into 4 bytes and then send them. -- Chris Johnson
On Tue, 2005-03-15 at 23:57 -0600, Chris Johnson wrote:
You do realize that the format of the integer "c" might well be different on the writing system and on the receiving system, don't you? Big endian versus little endian, or 2's-complement versus 1's-complment, for example.
This is why RPC libraries always define external data formats (XDF), so that every process agrees on the same 4-byte representation of an integer.
Having said that, take a look at the socket_send() page in the user comments. I think you will have to do your own conversion of the integer into 4 bytes and then send them.
Yes I do realise, and it is sad that I have come in on this project so late. The first thing that I said was that the protocol was crap. And I do realise that there is going to be possible endness issues. Thanks. -- Gordon Heydon <gordon@heydon.com.au>
Do you mean fgets($handle, 4096); fgets and fread are binary safe as I know. But there is no function to calculate the binary size of the data. You have to do this manually. Carl McDade Gordon Heydon wrote:
Hello,
I know that this may be a little off topic, but I know most people here deal with php most of the time. I need to write a little test suite, and the only thing that is stopping this from being easy is that I need to know how to write a raw integer (4 binary chars) into a socket.
I can do this with c, it is just...
int c;
write(sockfd, c, sizeof(int));
So what would this be in php.
I was wondering if someone can shed some light on it.
Thanks in advance.
fgets and fread are binary safe as I know. But there is no function to calculate the binary size of the data. You have to do this manually.
From the User Contributed Notes on fwrite: note that fwrite() is binary-safe with strings only - you cannot write binary data with it (ie 32-bit integers, 1 (d)word etc) Trying to fwrite() an int will cause the int to be converted into string and then written as string.
Hmm, Scratch that then. Second suggestion. Create a shared object in C and call it from PHP. Caveat though writing C so for Zend engine is not for the meek mild mannered C programmer. You have to be tough ;) I have only done this on windows so I can't help you with Linux. Carl McDade Karoly Negyesi wrote:
fgets and fread are binary safe as I know. But there is no function to calculate the binary size of the data. You have to do this manually.
From the User Contributed Notes on fwrite:
note that fwrite() is binary-safe with strings only - you cannot write binary data with it (ie 32-bit integers, 1 (d)word etc) Trying to fwrite() an int will cause the int to be converted into string and then written as string.
I know this is possible in VBscript so PHP being more powerful should be able to do it. It might take some thinking though. Here's a 2binary string converter: http://www.phpfreaks.com/quickcode/code/244.php That is the one part done. Carl McDade Karoly Negyesi wrote:
fgets and fread are binary safe as I know. But there is no function to calculate the binary size of the data. You have to do this manually.
From the User Contributed Notes on fwrite:
note that fwrite() is binary-safe with strings only - you cannot write binary data with it (ie 32-bit integers, 1 (d)word etc) Trying to fwrite() an int will cause the int to be converted into string and then written as string.
Gordon Heydon wrote:
Hello,
I know that this may be a little off topic, but I know most people here deal with php most of the time. I need to write a little test suite, and the only thing that is stopping this from being easy is that I need to know how to write a raw integer (4 binary chars) into a socket.
I can do this with c, it is just...
int c;
write(sockfd, c, sizeof(int));
So what would this be in php.
I was wondering if someone can shed some light on it.
Thanks in advance.
I have experience interfacing with a PICK system. It was running on an ancient Digital Unix box without a modern compiler, so without a serious upgrade, very few options existed. I ended up writing a minimalistic HTTP server in C that spoke via UNIX file sockets to a PICK process. The beauty of this solution for me was that I could accommodate a clueless PICK developer. All she had to do was open a "file" and read. When a newline came in, she would process the data packet. I did all of my heavy logic in a Python script on a remote server (yours would be a drupal server). Since then, I know PICK was movinig toward Linux support, and you might be able to use php to write to/read from a Unix file socket from a local apache/php server. I was sending financial transaction data using this connection method, and there was a 2-step transaction confirmation system built into it. Your needs may not go this far, but such a design is possible. If your needs are simple and the PICK server is on the same box as your drupal server, you can go straight into file sockets and talk directly to it from drupal. My knowledge of PICK and its is about 6 years old, so my advice might be somewhat dated and there may be better options available. Good luck with this project, -Mark
On Tue, 2005-03-15 at 08:24 -0600, Mark wrote:
Gordon Heydon wrote:
I have experience interfacing with a PICK system. It was running on an ancient Digital Unix box without a modern compiler, so without a serious upgrade, very few options existed. I ended up writing a minimalistic HTTP server in C that spoke via UNIX file sockets to a PICK process. The beauty of this solution for me was that I could accommodate a clueless PICK developer. All she had to do was open a "file" and read. When a newline came in, she would process the data packet. I did all of my heavy logic in a Python script on a remote server (yours would be a drupal server).
There are some commercial products which allow you to interface a pick system to the web such as a product that I do alot of work with from IBM called RedBack which interfaces with their PICK products UniData and UniVerse. It is actually quite a big area, making up 40% of their SMB business. The problem is that it only talks to jsp and asp. so I life is hell, as I have to do alot of work with redback.
Since then, I know PICK was movinig toward Linux support, and you might be able to use php to write to/read from a Unix file socket from a local apache/php server. I was sending financial transaction data using this connection method, and there was a 2-step transaction confirmation system built into it. Your needs may not go this far, but such a design is possible.
Yes it has very good linux support, and runs very fast. The biggest problem is a serious lack of php support.
If your needs are simple and the PICK server is on the same box as your drupal server, you can go straight into file sockets and talk directly to it from drupal. My knowledge of PICK and its is about 6 years old, so my advice might be somewhat dated and there may be better options available.
The biggest problem is that the person who designed the protocol doesn't really know PICK and it bad handing of binary data, or how to design a socket protocol. He put in a single binary int into a text stream. I am still shaking my head.
Good luck with this project, -Mark
Thanks for this. and Thanks to everyone for their suggestions. I think I am going to need to write a function to convert it myself. -- Gordon Heydon <gordon@heydon.com.au>
What about: $string = chr($num >> 24) . chr(($num >> 16) & 0xFF) . chr(($num >> 8) & 0xFF) . chr($num & 0xFF); (big endian) Depending on how chr works, you might even get away with removing the & 0xFF. Steven Wittens
participants (6)
-
Carl McDade -
Chris Johnson -
Gordon Heydon -
Karoly Negyesi -
Mark -
Steven Wittens