The socket data is received, but the data on the binding server is always sent, why only accept a string

Source: Internet
Author: User
Tags set time socket error
Send the data is so @,13,56,89,5,21,64,79,51,46,31,$, I receive this is the
{"IS_OK": 1, "Data": @,13,56,89,5,21,64,79,51,46,31,$}
But the data is always hair, although all are the same string of data, but I only accept a string, there is no, how to define the beginning and end of the data sign!! Want to add to the database on the server to see, but how to turn into JSON data format Ah! Send data format to how to change, because later to use Ajax to get data to Highchart, how to call this string of data Ah! Is it written as a JSON array!

/*------------------------------------------------------*///--Socket client/*-------------------------------------- ----------------*///+error_reporting (0); set_time_limit (0); Receive GET Data $msg = isset ($_get[' msg ")? Trim ($_get[' msg '): ';    Socket error code function strerror ($code) {$str = ';            Switch ($code) {case 10022: $str = ' parameter error ';        Break Case 10048: $str = ' typically each socket address (Protocol/network address/port) is allowed only once.            ';        Break Case 10061: $str = ' The connection failed because the server was actively refused!            ';        Break            Default: $str = ' unknown error ';    Break } return $str;} function Get_server_config () {return simplexml_load_file (' Include/server_config.xml ');} Get socket server configuration information $server_conf = Get_server_config (), $ip = $server _conf->ip; $port = (int) $server _conf->port; $out _str = ' {' IS_OK ': 0, ' data ': ';//Create socketif (($socket = Socket_create (Af_inet, sock_stream, sol_tcp) = = = = False) {$o Ut_str. = ' "'. Strerror (Socket_last_error ()). '"}';} Connect to server else if (Socket_connect ($socket, $ip, $port) = = = = False) {$out _str. = ' "'. Strerror (Socket_last_error ($socket)). '"}';} Sends the request to the server else if (Socket_write ($socket, $msg, strlen ($msg)) = = = = False) {$out _str. = ' "'. Strerror (Socket_last_error ($socket)). '"}';} else if ($out = Socket_read ($socket, 8192) = = = = False) {$out _str. = ' "'. Strerror (Socket_last_error ($socket)). '"}';} else {$out _str = ' {' IS_OK ': 1, ' data ': '. $out. '}';} echo $out _str; Close Socketsocket_close ($socket); /*------------------------------------------------------*///--Socket Server/*-------------------------------------- ----------------*/include (' include/init.php ');//Set time zone//date_default_timezone_set (' etc/gmt-8 '); Echo str_repeat (", 4000); error_reporting (0); set_time_limit (0); Ob_start (); Displays the hint message function Show_tip ($msg, $is _ok=true) {if ($is _ok) {$msg = '. $msg.    ''; } else {$msg = '. $msg.    '';    } Echo ';    Ob_flush ();    Flush (); Sleep (1);} Get socket service SideConfiguration Information $server_conf = Get_server_config (), $ip = $server _conf->ip; $port = (int) $server _conf->port; $server _name = $ server_conf->name; Show_tip ($server _name. ' Initializing ... '); Create Socketif (($socket = Socket_create (Af_inet, sock_stream, sol_tcp) = = = False) {Show_tip ($server _name. ' Creation failed, Reason: '. Strerror (Socket_last_error ()), false);} else {show_tip ($server _name. ' Created successfully! ');} Bind the socket to the specified IP, port if (Socket_bind ($socket, $ip, $port) = = = False) {Show_tip (' bind '. $server _name. ' Failure, Reason: '. Strerror (Socket_last_error ($socket)), false);} else {show_tip (' bind '). $server _name. ' To '. $ip. ', ' . $port);} Listen if (Socket_listen ($socket, 5) = = = False) {Show_tip ($server _name. ' Monitoring failed, Reason: '. Strerror (Socket_last_error ($socket)), false);} else {show_tip ($server _name.    ' Listening in ... ');    Echo ';    Ob_flush (); Flush ();}    static $w _socket = null;    Webstatic $a _socket = null; android $rNodeDataModel = new Nodedatamodel (); do {if ($msg _socket = socket_accept ($SOCKET) = = = False) {Socket_close ($msg _socket);    Break             } else {if ($buffer = Socket_read ($msg _socket, 8192)!== false) {$buffer = Trim ($buffer);            if ($buffer = = ' web ') {$w _socket = $msg _socket;            } else if ($buffer = = ' Android ') {$a _socket = $msg _socket;                } else if ($buffer! = ' Stop ') {$str _arr = explode (' & ', $buffer); if (!empty ($str _arr[1])) {$c _name = Trim ($str _arr[0]);//client identity $c _data = Trim ($str _ARR[1]); Client data if ($c _name = = ' computer ') {//Determine if the data is in JSON format if (                                ($r _arr = Json_decode ($c _data, true)) = null) {if ($w _socket! = null) {                            Socket_write ($w _socket, $c _data, strlen ($c _data));                            } if ($a _socket! = null) {    Socket_write ($a _socket, $c _data, strlen ($c _data));                                }//Add to Database foreach ($r _arr as $v) {                                $v [' node_id '] = $v [' point '];                                unset ($v [' point ']);                                $v [' create_time '] = time ();                            $rNodeDataModel->insert ($v);             }}}} socket_close ($msg _socket);            } else {socket_close ($msg _socket);        }} else {socket_close ($msg _socket); }}} while ($buffer! = ' Stop '); Closure of Socketsocket_close ($w _socket); Socket_close ($a _socket); Socket_close ($socket);


Reply to discussion (solution)

Once you ask, you naturally get only one data

Once you ask, you naturally get only one data

How to ask the moderator, how many requests, there is time interval how to write, with no definition that string of data start and end tag Ah! Thank you Moderator

Once you ask, you naturally get only one data

If you want to add that string of data to the database, how do you change the format (the original or received) and the call,

You can get the data multiple times by invoking the client program multiple times.
How to put data into a database depends on how you save it in your database
The transmitted data format should be designed according to your usage requirements

You can get the data multiple times by invoking the client program multiple times.
How to put data into a database depends on how you save it in your database
The transmitted data format should be designed according to your usage requirements


function Connect () {$.ajax ({url: ' client.php?msg=web ', type: ' Get ', DataType: ' JSON ', success:function (Result) {//  The data returned by the server, processed according to the DataType parameter, and a string describing the state. $ (' #tb tr:gt (0) '). Remove ();//Delete the previous data if (RESULT.IS_OK) {var x = (new Date ()). GetTime (); $.each (Result.data, function (i) { var index = parseint (result.data[i].point)-1;chart01.series[index].addpoint ([X, Parsefloat (Result.data[i])], true, true); Chart02.series[index].addpoint ([X, Parsefloat (Result.data[i])], true, true); Chart03.series[index].addpoint ( [X, Parsefloat (Result.data[i])], true, true); Chart04.series[index].addpoint ([X, Parsefloat (Result.data[i])], true, true); Chart05.series[index].addpoint ([X, Parsefloat (Result.data[i])], true, true); Chart06.series[index].addpoint ( [X, Parsefloat (Result.data[i])], true, true); Chart07.series[index].addpoint ([X, Parsefloat (Result.data[i])], true, true); Chart08.series[index].addpoint ([X, Parsefloat (Result.data[i])], true, true); Chart09.series[index].addpoint ( [X, Parsefloat (Result.data[i])], True, True); Chart10.series[index].addpoint ([X, Parsefloat (Result.data[i])], true, true);},1000);//Here is automatic data fetch at intervals} else {error =        True;layer.alert (result.data);} },complete:function (XHR, TS) {//The callback function after the request is completed (called after the request succeeds or fails). XHR = null; Release the XMLHttpRequest object if (!error) connect ();});} function Create_data () {var data = [],time = (new Date ()). GetTime (), i;for (i = -19; I <= 0; i++) {Data.push ({x:time + I * 1000});} return data;} var chart01, chart02, chart03, chart04, chart05, chart06, chart07,chart08,chart09,chart10;function create_chart (ID, T, y _t) {var chart = new Highcharts.chart ({chart: {renderto:id,type: ' spline ', Animation:highcharts.svg,backgroundcolor: ' # 272735 ', events: {load:connect}},title: {text:t},xaxis: {type: ' DateTime ', Tickpixelinterval:150},yaxis: {title: {text : y_t},plotlines: [{//Line value:0,width:1,color: ' #808080 '}]},tooltip: {formatter:function () {return '' + this.series.name + '
' +highcharts.dateformat ('%y-%m-%d%h:%m:%s ', this.x);}},series: [{name: ' Live Data ', data: [29.9,-71.5, 106.4, 15.2, 144.0, 258.0, 135.6, 177.5, 216.4, 194.1, 95.6, 54.4]//create_data ()}],credits:{Enabled:false}}); return chart;}
How to assign the data to each chart table, {"IS_OK": 1, "Data": [13,56,89,5,21,64,79,51,46,31]}, how to represent it in the series data. Hope to give some substantive advice. Thank you

It is recommended that you read the Ajax paradigm of Highcharts carefully.
Must be more professional than I am.

is to put the value of data, how to take it out don't know, like result.data[i] this form

Your code request once is of course a result

Your code request once is of course a result

I opened the site, only open once Ah! Keep on refreshing to request AH. So add this.
$.each (Result.data, function (i) {
var index = parseint (result.data[i].point)-1;

Chart01.series[index].addpoint ([x, Parsefloat (Result.data[i]), true, true);
Chart02.series[index].addpoint ([x, Parsefloat (Result.data[i]), true, true);
Chart03.series[index].addpoint ([x, Parsefloat (Result.data[i]), true, true);
Chart04.series[index].addpoint ([x, Parsefloat (Result.data[i]), true, true);
Chart05.series[index].addpoint ([x, Parsefloat (Result.data[i]), true, true);
Chart06.series[index].addpoint ([x, Parsefloat (Result.data[i]), true, true);
Chart07.series[index].addpoint ([x, Parsefloat (Result.data[i]), true, true);
Chart08.series[index].addpoint ([x, Parsefloat (Result.data[i]), true, true);
Chart09.series[index].addpoint ([x, Parsefloat (Result.data[i]), true, true);
Chart10.series[index].addpoint ([x, Parsefloat (Result.data[i]), true, true);
},1000)//1000 data is automatically taken at every time
Key how the value of data gives coordinates chart1.series[index].addpoint ([x, Parsefloat (Result.data[i])], true, true);
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.