Jump to content

Send an array using sockets


Go to solution Solved by .josh,

Recommended Posts

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!!

Link to comment
https://forums.phpfreaks.com/topic/282758-send-an-array-using-sockets/
Share on other sites

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.

$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.

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?

  • Solution

(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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.