Hello All,

I am trying to write some PHP code that will act as a client to a
homegrown server interface developed in C. The client/server interfaces
exchange data using two C structures defined as:

/* struct for client requests to server */

struct client_request {
        char stringA[20];
        char stringB[17];
        char stringC[10];
        char stringD[7];
        char stringE[200];
        char stringF[12];
        char stringG[11];
        char stringH[50];
        char stringI[11];
        short  numA;
        short  numB;
};

/* struct for server replies to client */

#define REPLY_MSG_SIZE 4000

struct reply_msg {
        short numA;
        short numB;
        short numC;
        short data_len;
        char data[REPLY_MSG_SIZE + 1];
};

I am able to use pack() successfully to post a request message to the
server, but the unpack() function never seems to work on the reply
message struct that gets returned to the client.

Here's my example of the pack() function that works correctly:

    $p = pack("a20a17a10a7a200a12a11a50a11ss", $strA, $strB, $strC,
                                               $strD, $strE, $strF,
                                               $strG, $strH, $strI,
                                               $numA, $numB);

    $fp = fsockopen($host, $port);
    if ($fp) {
        $serv_stat = fgets($fp, 3);
    }
    if ($serv_stat == "UP") {   // server connected and awaiting msg...
        $nb = fwrite($fp, $p, strlen($p));
        print("Wrote $nb bytes.\n");

        $servResp = fread($fp, 3);
        print("[" . $servResp . "]\n");
    }


Now once the server receives the request successfully (which it does),
it will reply back to the client using the second struct, and this is
where I begin having problems with unpack():

.
.
.
   do {
       if (false == ($buf = socket_read($msgsock, 2048,
PHP_BINARY_READ))) {
           echo "socket_read() failed: reason: " . socket_strerror($ret)
. "\n";
           break 2;
       }
       if (!$buf = trim($buf)) {
           continue;
       } else {
           printf("Read %d bytes.\n", strlen($buf));
           
           $format =
"@0/sval0/@1/sval1/@2/sval2/@3/sval3/@4/sval4/@5/sval5/@6/C*";

           $respData = unpack($format, $buf);

           print_r($respData);
       }
   }
.
.
.

My first thought was that it had something to do with the "endian-ess"
between the client machine and the server system. But, I have tried just
about every possible combination of formatting codes, all to no avail.
The client is a Solaris SPARC platform and the server is running on a
mainframe (big endian).

What is strange is that I can always get the "char data[]" structure
member, and sometimes I get meaningful data in a few of the shorts, but
never all of the structure's members at the same time.

Any help is appreciated.

-- 
Scott E. Young
Area Mgr - NMA Software Solutions
[EMAIL PROTECTED]
(713) 567-8625

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to