English 中文(简体)
php sockets read json array from java server
原标题:

I am connecting to a server written in JAVA using TCP/IP. My application sends json arrays to this server and in some cases also expects some results, json arrays. The problem is that i can easily send json via tcp but when reading it the script freezes waiting forever until it timeouts. Here is my code.

$sock = socket_create(AF_INET, SOCK_STREAM, 0)  //Creating a TCP socket
                or die("error: could not create socket
");
        $succ = socket_connect($sock, Application_Model_Config::serverHost, Application_Model_Config::serverPort)   //Connecting to to server using that socket
                or die("error: could not connect to host
");

        socket_write($sock, $send. 
 , strlen($send)+1);

        $response =   ;
        while ($resp = socket_read($sock, 1024)) {
            if (!$resp)
                break;
            $response .= $resp;
            if (strpos($resp, "
") !== false)
                break;
        }

        echo "Server said: {$response}";
    }

$send is a an array encoded as json_encode($array).

Sending is ok but when needed to receive i don t get anything.

I wouldn t mind handling this using jquery (sending and getting json objects from the server) if that would be possible. I am not aware of any implementation that achieves something like this but i m opened to suggestions...actually would prefer it instead of php.

问题回答

In the mode you re using socket_read, it has the same semantics as recv:

If no messages are available at the socket, the receive calls wait for a message to arrive, unless the socket is nonblocking (see fcntl(2) ), in which case the value -1 is returned and the external variable errno set to EAGAIN. The receive calls normally return any data available, up to the requested amount, rather than waiting for receipt of the full amount requested.

Therefore, if the script is "waiting forever until it timeouts" that s because there s no data to read. You can confirm this with a packet sniffer.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签