HELP: send binary replies back and forth ???

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

My php application (on Apache/Linux) needs to do the following:

The PHP script receives a request from a client (binary), asking for certain  
records of data.
My PHP script loops through all records and sends each of them ONE BY ONE.  
After each record that my server script sends, it waits for the client to  
confirm proper reception with an ACK (binary digit).
When there are no more records, my server script sends the client a binary  
digit 0.

So the client initiates a connection to the webserver with my php script,  
and communication then goes back and forth between the server and client a  
couple of times before a 'session' completes.
Looks like this:

1. client ---> server (client asks for records)
2. client <--- server (server sends record 1)
3. client ---> server (client confirms with ACK)
4. client <--- server (server sends record 2)
5. client ---> server (client confirms with ACK)
6. client <--- server (server sends ACK to notify end of records)

Additional info:
1. The client is not a webbrowser but a piece of simple hardware that can  
only do simple TCP/IP connections and doesn't come with any specific  
internet application protocol such as HTTP. HTTP is easy enough to implement  
just to exchange binary data, but there's no reason to use HTTP other than  
to be able to communicate with apache.
2. The reason I have to send records one by one is because the client is a  
special device with limited read buffer.
3. All communication is binary because the connection used is pricy and  
charged by kilobytes of data.


1. A webserver usually doesn't communicate back and forth, rather client  
sends request, server sends reply, and connection is closed. Also, HTTP  
headers generate extra overhead that I really don't need and want (generates  
extra data that we get charged for). How can I accomplish communication like  
the above with apache and php?

I'm afraid I may have to write my own PHP style socket server without the  
use of apache at all. Then it wouldn't have to be HTTP but all binary  
communication only. The problem with this is that I think it might be hard  
to develop something robust in just 2 days. A webserver has a proven track  
record, has security features (think of DOS attacks) and other things that  
would be hard to implement myself. But all my script would need to do is the  
above so it doesn't really need much.
I don't know how hard it would be to develop a php server that would handle  
concurrent connections and keep running, and is secure enough that it  
woudn't allow DOS attacks or worse, allow execution of commands on the linux  
machine with root privileges. chrooting and other things is something I'm  
not experienced enough with to trust. So I'd like to hide behind apache if  
possible. But communicating binary data without HTTP headers has proven  
impossible from within apache.

Also, how can my script RESPOND in binary data?
"echo 0" doesn't send the byte value 0, rather the ascii value "0".
passthru allows binary data to be outputted but only in response to some  
shell command.
And I guess "echo pack("C", 0)" wouldn't do it either.

If the client could also act as a server, then I could've simply opened up a  
socket in response to the server and communicated back and forth with it in  
a loop, in a single call via apache.

Example quasi code that illustrates what I'd like to be able to do.


$size = strlen($binary);
$arr = unpack( .... , $binary);

if ($arr['request-type'] == 5) {

    $records = getBinaryRecords();
    foreach($records as $rec) {
        while (awaitReply() != ACK) {
    sendBinary(ACK); // done

Re: HELP: send binary replies back and forth ???

Lisa Pearlson wrote:

Quoted text here. Click to load it


I think your proposed setup will run into trouble.
You make a loop that should wait for a response from your mysterydevice  
(Let's call it MD from now :-).  
PHP typically responds to 1 request.
PHP also has a default 'scripttimeout' of 30(?) seconds, but that can  
probably be modified by you via php.ini.

I think creating a session makes more sense, but that can prove difficult  
because your MD probably doesn't support http and cookies *I think*.

Personally I would opt for a solution without PHP.
Perl and Java might be much more suitable for your situation.

About the 'hiding behind apache' for security: Yes, afaik apache is  
rocksolid (as long as you don't use obscure modules), but is you MD?
Will your MD have a fixed IP-adres?
If so, you can easily confige Java/Perl/PHP to only communicate with that  

You probably need some authentication too.
I guess you do not want some freak sending data to your MD.
If you can, use SSH2 or something, but I do not know if that will give you a  
lot of extra datatraffic.

Just my 2 cents.

Erwin Moller

PS: Just curious: Are you building a sattelite? :P

Re: HELP: send binary replies back and forth ???

You're right. my MD satellite device can not support those advanced features  
(actually is a GPRS device), but security on that side is not an issue. It's  
a client, not a server. And I will use MD5 (and I don't mean Mystery Device  
5 here :)) for minimal but fairly robust way to verify data integrity with a  
secret password as part of the soup that MD5 is applied on.

Anyway, forget about all that... It does look like I won't be able to use  
apache (even though the timeout won't be an issue.. and yes, you can use a  
simple php function to set the timeout), .. but there's no reason not to use  
php instead of perl/java.

I know you can write whole webservers in php (see nanoweb /) but I need something more simple.

Issues in writing php server are:
- chroot / run under certain user privileges that would disallow someone to  
execute shell commands.
- ensure certain number of concurrent connections but not make it vulnerable  
to DOS attacks.
- server start/stop scripts


"Erwin Moller"  
Quoted text here. Click to load it

Site Timeline