PHP87 Posted October 6, 2013 Share Posted October 6, 2013 I'm new to the forum and I take this opportunity to say hello at everyone.I'm not an expert on php, but even a neophyte. That being said let's move to the problem, as mentioned in the title concerns the socket. Before putting the parts of the code that interest me start by saying that if to be sent through the socket is a simple sentence and not an array everything is working correctly, then the problem occurs only with an array.1) client.php: $array = array('Say' => 'hello'); $socket = socket_create(AF_INET, SOCK_STREAM, 0); socket_connect($socket, $host, $port); socket_write($socket, $array, strlen($array)); $result = socket_read ($socket, 1024); 2)server.php: $output = array(); while(1){ $spawn = socket_accept($socket); $input = socket_read($spawn, 1024); array_push($output, $input); socket_write($spawn, $output, strlen ($output)); } Personally I think there is more than one error, first I would say that for example $ input is not an array, but it must contain a value that is an array, then would start already from here, also do not know if it is possible to send an array using sockets.And 'possible or do I have to split the array and I have to send the data twice? Translate from google, sorry for bad english. Bye!! Quote Link to comment Share on other sites More sharing options...
vinny42 Posted October 6, 2013 Share Posted October 6, 2013 Sockets are a generic TCP/IP thing, they have no concept of "array". If you want to send an array of data then you will have to transform the array into some binary format, send it across, and tansform it back. Look at serialise() and json_encode(). Quote Link to comment Share on other sites More sharing options...
PHP87 Posted October 6, 2013 Author Share Posted October 6, 2013 Thank you for response, i choose json_encode() and json_decode(). Last question: $json = json_decode($json); $array = array('one' => 'one'); $array= json_encode($array); $array= json_decode($array); array_push($json, $array); Why array_push don't work? Quote Link to comment Share on other sites More sharing options...
vinny42 Posted October 6, 2013 Share Posted October 6, 2013 it does, but you are trying to push an array into itself. Stop uising the same variable name for everything. Quote Link to comment Share on other sites More sharing options...
PHP87 Posted October 6, 2013 Author Share Posted October 6, 2013 $json_decode = json_decode($json); $array = array('one' => 'two'); $array_two = json_encode($array); $array_three = json_decode($array_two); array_push($json_decode, $array_three); Same problem. Quote Link to comment Share on other sites More sharing options...
kicken Posted October 6, 2013 Share Posted October 6, 2013 Depending on what $json contains before you decode it, your $json_decoded may be an object not an array. If you want it to use associative arrays rather than objects then you need to pass true as the second parameter to json_decode. Quote Link to comment Share on other sites More sharing options...
.josh Posted October 7, 2013 Share Posted October 7, 2013 Also to be clear, client.php should encode it. server.php should decode it. Quote Link to comment Share on other sites More sharing options...
Zane Posted October 7, 2013 Share Posted October 7, 2013 $array = array('one' => 'one'); $array= json_encode($array); $array= json_decode($array); All this does is create and array, encode it, and decode it. In other words, you are turning it into a sendable format, then changing it back to an array. Completely anti-productive if you ask me. Also to be clear, client.php should encode it. server.php should decode it. Quote Link to comment Share on other sites More sharing options...
PHP87 Posted October 7, 2013 Author Share Posted October 7, 2013 Thank you very much. Now finally works. I create 3 pages: server.php, client.php and take_message,php. I have a problem now only whit this my two-easy script(1:index.php, 2:client.php): setInterval( function example(){ $.ajax({ type: "POST", url: "../chat/client.php", dataType: "html", success: function(html){ $("#read_chat").html(html); } }); },100); <?php $host = '127.0.0.1'; $port = 25003; $message = $_POST['message']; $socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n"); socket_connect($socket, $host, $port) or die("Could not connect to server\n"); socket_write($socket, $message, strlen($message)) or die("Could not send data to server\n"); $result = socket_read ($socket, 1024) or die("Could not read server response\n"); echo $result; socket_close($socket); unset($message); ?> All two work exactly, but the read page is very slowed. How can I keep the page client.php waiting for a value different from the previous one? Quote Link to comment Share on other sites More sharing options...
Solution .josh Posted October 7, 2013 Solution Share Posted October 7, 2013 (with javascript) you can either set a global variable or else a static variable (e.g. hasUpdated)within the example() function and instead of having example() make the ajax request every time it is called, check if hasUpdated has changed. If it has, then execute the ajax call. If not, do nothing. Then within the success: callback, increment or otherwise change hasUpdated. Alternatively... instead of wrapping example() in a setInterval(), just make the success: function call example(). Either way, you may want to add a error: callback to the ajax call that attempts to call example() a set number of times or whatever, before stopping for good (or display an error to the user, etc.). The 2 methods above will make it to where if the ajax call fails, no further ajax requests will be made. Quote Link to comment Share on other sites More sharing options...
PHP87 Posted October 10, 2013 Author Share Posted October 10, 2013 Solved. Thank you! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.