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.