I receive socket data, but the bound server keeps sending data. why do I receive only one string?

Source: Internet
Author: User
Tags socket error
I receive socket data, but the data on the bound server is sent all the time. why do I receive only one string of sent data? @, $, this is what I receive.
{"Is_ OK": 1, "data": @, $}
However, the data is always sent, although it is all the same string of data, but I only accept a string, there will be no later. how to define the start and end signs of the data !! I want to add it to the database on the server, but how can I convert it to the json data format! How can I change the data sending format? because ajax is used to add data to highchart. how can I call this data string! Is it a json Array!

/* Optional * // -- 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 = 'generally, each socket address (Protocol/network address/Port) can be used only once. '; Break; case 10061: $ str =' connection failed because the server refused actively! '; Break; default: $ str = 'unknown error'; break;} return $ str;} function get_server_config () {return simplexml_load_file ('include/server_config.xml ');} // Obtain the 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) {$ out_str. = '"'. strerror (soc Ket_last_error ()). '"}';} // connect to the server else if (socket_connect ($ socket, $ ip, $ port) ===false) {$ out_str. = '"'. strerror (socket_last_error ($ socket )). '"}';} // send the else if (socket_write ($ socket, $ msg, strlen ($ msg) request to the server === false) {$ out_str. = '"'. strerror (socket_last_error ($ socket )). '"}';} else if ($ out = socket_read ($ socket, 8192) == false) {$ out_str. = '"'. strerror (socket_last_error ($ so )). '"}';} Else {$ out_str = '{" is_ OK ": 1," data ":'. $ out. '}';} echo $ out_str; // close socketsocket_close ($ socket);/* sockets * // -- socket server/* sockets */include ('include/init. php '); // Set the time zone // date_default_timezone_set ('etc/GMT-8'); echo str_repeat ('', 4000); error_reporting (0); set_time_limit (0); ob_sta Rt (); // display the prompt information function show_tip ($ msg, $ is_ OK = true) {if ($ is_ OK) {$ msg = ''. $ msg. '';} else {$ msg = ''. $ msg. '';} echo 'script parent. $ ("# tip "). showTip ("'. date ('Y-m-d H: I: S', time ()). '","'. $ msg. '"); script'; ob_flush (); flush (); sleep (1);} // Get the socket server configuration information $ server_conf = get_server_config (); $ ip = $ server_conf-> ip; $ port = (int) $ server_conf-> port; $ server_name = $ server_conf-> nam E; show_tip ($ server_name. 'initializing... '); // create socketif ($ socket = socket_create (AF_INET, SOCK_STREAM, SOL_TCP) === false) {show_tip ($ server_name. 'creation failed because :'. strerror (socket_last_error (), false);} else {show_tip ($ server_name. 'created successfully! ');} // Bind the socket to the specified IP address. if (socket_bind ($ socket, $ ip, $ port) === false) {show_tip ('bine '. $ server_name. 'failure, cause :'. strerror (socket_last_error ($ socket), false);} else {show_tip ('bine '. $ server_name. ''. $ ip. ','. $ port);} // listen to if (socket_listen ($ socket, 5) ===false) {show_tip ($ server_name. 'listening failed because :'. strerror (socket_last_error ($ socket), false);} else {show_tip ($ server_name. 'Monitoring Listening... '); echo' script location. href = "login. php "; script '; 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 ID $ c_data = trim ($ str_arr [1]); // client data if ($ c_name = 'computer ') {// Determine whether 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) ;}} while ($ buffer! = 'Stop'); // close socketsocket_close ($ w_socket); socket_close ($ a_socket); socket_close ($ socket );


Reply to discussion (solution)

You only get data once.

You only get data once.

Which of the following statements can I ask the moderator how to write multiple requests and time intervals? you don't need to define the start and end mark of the string of data! Thank you moderator

You only get data once.

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

After you call the client program multiple times, you can receive the data multiple times.
How to put data into the database depends on how your database is saved.
The format of transmitted data should be designed according to your needs.

After you call the client program multiple times, you can receive the data multiple times.
How to put data into the database depends on how your database is saved.
The format of transmitted data should be designed according to your needs.


Function connect () {$. ajax ({url: 'client. php? Msg = web', type: 'GET', Ype: 'json', success: function (result) {// the data returned by the server and processed according to the dataType parameter; A string that describes the status. $ ('# 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]. addPo Int ([x, parseFloat (result. data [I])], true, true); chart10.series [index]. addPoint ([x, parseFloat (result. data [I])], true, true) ;}, 1000); // here the data is automatically retrieved at intervals} else {error = true; layer. alert (result. data) ;}, complete: function (XHR, TS) {// callback function after the request is complete (called after the request is successful 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: 'real-time 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 obtained data to each chart table, {"is_ OK": 1, "data": [,]}, how to display data in series. I hope to give some practical suggestions. Thank you.

We recommend that you carefully read the Highcharts ajax examples.
Definitely more professional than me

I don't know how to take out the data value, such as result. data [I ].

One request by your code is of course a result.

One request by your code is of course a result.

I open the website only once! Requests are continuously sent only when refresh is not stopped. So I added 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); // The data is automatically retrieved at intervals of 1000.
The key is how to give the data value to the coordinate chart1.series [index]. addPoint ([x, parseFloat (result. data [I])], true, true );

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.