I am working on a project to send sensor data to phpmyadmin table using GET request.
I am not able to see sensor data in the table when I consider my Arduino to be the client, but when I use this URL on my Google chrome browser it shows the result (ex. 40).
It seems the problem is with the Arduino code.
int samples[NUMSAMPLES];
void loop() {
// Thermistor
uint8_t i;
float average;
// take N samples in a row, with a slight delay
for (i=0; i< NUMSAMPLES; i++) {
samples[i] = analogRead(THERMISTORPIN);
delay(10);
}
// average all the samples out
average = 0;
for (i=0; i< NUMSAMPLES; i++) {
average += samples[i];
}
average /= NUMSAMPLES;
// convert the value to resistance
average = 1023 / average - 1;
average = SERIESRESISTOR / average;
float Steinhart;
Steinhart = average / THERMISTORNOMINAL; // (R/Ro)
Steinhart = log(Steinhart); // ln(R/Ro)
Steinhart /= BCOEFFICIENT; // 1/B * ln(R/Ro)
Steinhart += 1.0 / (TEMPERATURENOMINAL + 273.15); // + (1/To)
Steinhart = 1.0 / Steinhart; // Invert
Steinhart -= 273.15; // convert to C
Serial.print("Temperature ");
Serial.print(Steinhart);
Serial.println(" *C");
delay(5000);
Serial.println("\nStarting connection to server...");
// if you get a connection, report back via serial:
if (client.connect(server, 80)) {
Serial.println("connected to server");
// Make a HTTP request:
client.println("GET /add.php?");
client.print("Steinhart=");
client.print(Steinhart);
}
// if there are incoming bytes available
// from the server, read them and print them:
while (client.available()) {
char c = client.read();
Serial.write(c);
}
}
void setup() {
//Initialize serial and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
// check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
// don't continue:
while(true);
}
// attempt to connect to Wifi network:
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid);
// wait 10 seconds for connection:
delay(10000);
}
Serial.println("Connected to wifi");
printWifiStatus();
} // end of void setup()
And here is my PHP code: add.php file
<?php
include("connect.php");
$link=Connection();
$Steinhart = ""; // or null !!
$timeStamp="";
$Steinhart = isset($_GET['Steinhart']) ? $_GET['Steinhart'] : '';
date_default_timezone_set("Asia/Dubai");
$timeStamp = date('Y-m-d H:i:s', time());
$query= "INSERT INTO `time` (`id`, `timeStamp`, `Steinhart`) VALUES (NULL, '$timeStamp','$Steinhart')";
mysqli_query($link, $query);
mysqli_close($link);
?>
I Usually send data from the Arduino board to PhpmyAdmin(Wamp Server or XXamp) with the help of NodeJS. It is pretty easy to send data with the help of NodeJS.
Here i attach the code.
var request = require('request');
var serialport = require("serialport");
var SerialPort = serialport.SerialPort;
var serialPort = new SerialPort("COM5", {
baudrate: 9600,
parser: serialport.parsers.readline("\n")
});
serialPort.on("open", function () {
console.log('open');
serialPort.on('data', function(data) {
console.log(data);
});
});
serialPort.on('data', sendSerialData);
function sendSerialData(data) {
request({
uri: "http://127.0.0.1/write_data.php?value="+data,
method: "GET",
timeout: 10000,
followRedirect: true,
maxRedirects: 10
}, function(error, response, body) {
console.log(body);
});
}
By this you can easily send the data. Also you can just follow this link
http://www.instructables.com/id/PART-1-Send-Arduino-data-to-the-Web-PHP-MySQL-D3js/
Hope it will help
Related
I used Postman to debug my PHP POST API, and it works (ignoring the json versos non-json input).
<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
//
// include database and object files
require_once '../config/database.php';
require_once '../objects/resdatalog.php';
// instantiate database and resdatalog object
$database = new Database();
$db = $database->getConnection();
// initialize object
$rdl = new Resdatalog($db);
$rdl->Location = $_GET['Location'];
$rdl->Temperature = $_GET['Temperature'];
$rdl->Humidity = $_GET['Humidity'];
$rdl->Pressure = $_GET['Pressure'];
$rdl->RecDate = $_GET['RecDate'];
$rdl->AmbientTemp = $_GET['AmbientTemp'];
$rdl->AmbientHum = $_GET['AmbientHum'];
$rdl->AmbientPressure = $_GET['AmbientPressure'];
$ret=$rdl->insert();
?>
But, the client needs to be an ESP8266 module monitoring the environmentals in various locations. I have been trying to make that work, but debugging has just baffled me. No matter what I display on the client side, it looks OK. But it seems never to actually get to the web page. I put code in the web page to "echo" confirmations, but nothing shows up in the client. What is the best way to debug this setup:
/*
BME280 I2C Test.ino
This code shows how to record data from the BME280 environmental sensor
using I2C interface. This file is an example file, part of the Arduino
BME280 library.
GNU General Public License
Written: Dec 30 2015.
Last Updated: Oct 07 2017.
Connecting the BME280 Sensor:
Sensor -> Board
-----------------------------
Vin (Voltage In) -> 3.3V
Gnd (Ground) -> Gnd
SDA (Serial Data) -> A4 on Uno/Pro-Mini, 20 on Mega2560/Due, 2 Leonardo/Pro-Micro
SCK (Serial Clock) -> A5 on Uno/Pro-Mini, 21 on Mega2560/Due, 3 Leonardo/Pro-Micro
*/
#include <BME280I2C.h>
#include <Wire.h>
#include <ESP8266WiFi.h>
#include <Arduino.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#include <ArduinoJson.h>
#include <ezTime.h>
// ESP8266WiFiMulti WiFiMulti;
#define SERIAL_BAUD 115200
BME280I2C bme; // Default : forced mode, standby time = 1000 ms
// Oversampling = pressure ×1, temperature ×1, humidity ×1, filter off,
Timezone Charlotte;
String Location;
String Temperature;
String Humidity;
String Pressure;
String RecDate;
String AmbientTemp="0";
String AmbientHum="0";
String AmbientPressure="0";
const char* ssid = "mySSID"; // The SSID (name) of the Wi-Fi network you want to connect to
const char* password = "mySSIDPW"; // The password of the Wi-Fi network
//////////////////////////////////////////////////////////////////
void setup()
{
//set up the serial output
Serial.begin(SERIAL_BAUD);
for (uint8_t t = 4; t > 0; t--) {
Serial.printf("[SETUP] WAIT %d...\n", t);
Serial.flush();
delay(1000);
}
WiFi.begin(ssid, password); // Connect to the network
Serial.print("Connecting to ");
Serial.print(ssid); Serial.println(" ...");
int i = 0;
while (WiFi.status() != WL_CONNECTED) { // Wait for the Wi-Fi to connect
delay(1000);
Serial.print(++i); Serial.print(' ');
}
Serial.println('\n');
Serial.println("Connection established!");
Serial.print("IP address:\t");
Serial.println(WiFi.localIP()); // Send the IP address of the ESP8266 to the computer
waitForSync();
// Begin the BME setup:
Wire.begin(4,5);
while(!bme.begin())
{
Serial.println("Could not find BME280 sensor!");
delay(1000);
}
// bme.chipID(); // Deprecated. See chipModel().
switch(bme.chipModel())
{
case BME280::ChipModel_BME280:
Serial.println("Found BME280 sensor! Success.");
break;
case BME280::ChipModel_BMP280:
Serial.println("Found BMP280 sensor! No Humidity available.");
break;
default:
Serial.println("Found UNKNOWN sensor! Error!");
}
}
//////////////////////////////////////////////////////////////////
void loop()
{
printBME280Data(&Serial);
delay(500);
}
//////////////////////////////////////////////////////////////////
void printBME280Data(Stream* serialclient)
{
float temp(NAN), hum(NAN), pres(NAN);
BME280::TempUnit tempUnit(BME280::TempUnit_Fahrenheit);
BME280::PresUnit presUnit(BME280::PresUnit_inHg);
bme.read(pres, temp, hum, tempUnit, presUnit);
Charlotte.setLocation("America/New_York");
String localdatetime = Charlotte.dateTime("Y-m-d H:i:s");
serialclient->print("\tTemp: ");
serialclient->print(temp);
serialclient->print("°"+ String(tempUnit == BME280::TempUnit_Celsius ? 'C' :'F'));
serialclient->print("\t\tHumidity: ");
serialclient->print(hum);
serialclient->print("% RH");
serialclient->print("\t\tPressure: ");
serialclient->print(pres);
serialclient->print(" Inches Hg");
serialclient->println(" Date/Time: "+ localdatetime);
// Now, I'll try to build and post an HTTP command to my cloud API
//
Location="TiVo Cabinet";
Temperature=String(temp);
Humidity=String(hum);
Pressure=String(pres);
RecDate=localdatetime;
AmbientTemp=String(AmbientTemp);
AmbientHum=String(AmbientHum);
AmbientPressure=String(AmbientPressure);
String content =
"{\"Location\": \"" + String(Location) + "\"" +
" , \"Temperature\" : \"" + String(Temperature) + "\"" +
" , \"Humidity\" : \"" + String(Humidity) + "\"" +
" , \"Pressure\" : \"" + String(Pressure) + "\"" +
" , \"RecDate\" : \"" + String(RecDate) + "\"" +
" , \"AmbientTemp\" : \"" + String(AmbientTemp) + "\"" +
" , \"AmbientHum\" : \"" + String(AmbientHum) + "\"" +
" , \"AmbientPressure\" : \"" + String(AmbientPressure) + "\"" +
"}";
serialclient->println("Content: " + content);
// Now, we're gonna try to send this line to the server....
WiFiClient client;
if (!client.connect("telemetry.shafferassoc.com", 80)) {
Serial.println("WiFiClient connection failed");
delay(5000);
} else {Serial.println("WiFiClient connection OK");
}
client.println("Host: telemetry.shafferassoc.com:80\r\n");
client.println("POST TelemetryWebSite/API/Resdatalog/Insert.php HTTP/1.1");
client.println("Accept: */*");
client.println("Content-Length: " + String(content.length()));
client.println("Content-Type: application/json");
// client.println();
client.println(content);
Serial.println("Length: " +String(content.length()));
Serial.println(content);
Serial.println("receiving from remote server");
// not testing 'client.connected()' since we do not need to send data here
while (client.available()) {
char ch = static_cast<char>(client.read());
Serial.print(ch);
}
delay(5000);
}
Here is a snippet of the Serial output:
11:06:49.892 -> WiFiClient connection OK
11:06:49.892 -> Length: 204
11:06:49.892 -> {"Location": "TiVo Cabinet" , "Temperature" : "73.27" , "Humidity" : "0.00" , "Pressure" : "29.57" , "RecDate" : "2020-04-28 11:06:50" , "AmbientTemp" : "0" , "AmbientHum" : "0" , "AmbientPressure" : "0"}
11:06:49.933 -> receiving from remote server`
I post to server raw byte [] of texture but shows 5B in sql database and when this data is downloaded the file is empty. Can you please provide guidance?
I post to server the raw byte[] of a texture like so:
byte [] imgByte0 = uploadedTex.GetRawTextureData();//uploadTex is Texture2D
Debug.Log("Byte Len " + imgByte0.Length);//results in 722160
WWWForm form = new WWWForm();;
form.AddBinaryData("rgbImgBytes", imgByte0, "image/png");
UnityWebRequest www = UnityWebRequest.Post("https:address.php", form);
yield return www.SendWebRequest();
I post it to my php and insert into sql database BLOB (I'm aware there are other preferred alternatives to store images, imgs are/will be stufficiently small not the question here):
$thisRGBImg=$_FILES['rgbImgBytes'];
$stmt = $conn->prepare("INSERT INTO $imageTable `thisRGBImg` VALUES (?)");
$stmt->execute([$thisRGBImg];
Png image is 300Kb, however it shows as 5B held in sql database BLOB. I further confirm when I download these raw bytes to file (as per https://thoughtbot.com/blog/avoiding-out-of-memory-crashes-on-mobile#streams-to-the-rescue) and resulting file is empty:
using (UnityWebRequest myWebRequest = UnityWebRequest.Post(path, formData2))
{
myWebRequest.downloadHandler = new ToFileDownloadHandler(new byte[64 * 1024], savePathWithFileName);
yield return myWebRequest.SendWebRequest();
}
public ToFileDownloadHandler(byte[] buffer, string filepath) : base(buffer)
{
this.filepath = filepath;
fileStream = new FileStream(filepath, FileMode.Create, FileAccess.Write);
}
protected override bool ReceiveData(byte[] data, int dataLength)
{
if (data == null || data.Length < 1)
{
Debug.Log("ReceiveData - received a null/empty buffer");
return false;
}
received += dataLength;
Debug.Log("Data received " + dataLength + " total received " + received);
//if (!canceled) fileStream.Write(data, 0, dataLength);//replaced for bw below
if (!canceled)
{
var bw = new BinaryWriter(File.Open("path", FileMode.OpenOrCreate));
bw.Write(data);
bw.Flush();
bw.Close();
}
return true;
}
http://www.universalcard.byethost7.com is my server. Where I kept index.php file. Code is as given below
<?php
if(isset($_GET['username']) && isset($_GET['pin']) && isset($_GET['cost'])) {
$username = $_GET['username'];
$pin = $_GET['pin'];
$cost = $_GET['cost'];
$filecontent = "Username is: ".$username." and PIN is: ".$pin." and cost is: ".$cost."\n";
$filestatus = file_put_contents('uc.txt',$filecontent,FILE_APPEND);
if($filestatus != false )
{
echo "Data written to file..";
}else{
echo "Ohh sorry..";
}
} else {
echo "Something went wrong..";
}
?>
And I want to send a GET request from ESP8266 with Arduino IDE.
In this GET request, I am sending 3 variables 'username' , 'pin' and 'cost' with some values (data type is String). And these values are appending to a file "uc.txt". So when I send a request using a browser, values will append to the text file.
But when I tried to send using ESP8266 it is not appending
Arduino Code is below
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
const char* ssid = "rainbow";
const char* password = "12345678";
const char* host = "universalcard.byethost7.com";
const int httpsPort = 443;
// Use web browser to view and copy
// SHA1 fingerprint of the certificate
//const char* fingerprint = "CF 05 98 89 CA FF 8E D8 5E 5C E0 C2 E4 F7 E6 C3 C7 50 DD 5C";
void setup() {
Serial.begin(115200);
Serial.println();
Serial.print("connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
// Use WiFiClientSecure class to create TLS connection
WiFiClientSecure client;
Serial.print("connecting to ");
Serial.println(host);
if (!client.connect(host, httpsPort)) {
Serial.println("connection failed");
return;
}
String url = "/index.php?username=2bv14is114&pin=5555&cost=1111";
Serial.print("requesting URL: ");
Serial.println(url);
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"User-Agent: BuildFailureDetectorESP8266\r\n" +
"Connection: close\r\n\r\n");
Serial.println("request sent");
while (client.connected()) {
String line = client.readStringUntil('\n');
if (line == "\r") {
Serial.println("headers received");
break;
}
}
String line = client.readStringUntil('\n');
if (line.startsWith("{\"state\":\"success\"")) {
Serial.println("esp8266/Arduino CI successfull!");
} else {
Serial.println("esp8266/Arduino CI has failed");
}
Serial.println("reply was:");
Serial.println("==========");
Serial.println(line);
Serial.println("==========");
Serial.println("closing connection");
}
void loop() {
}
And the output in Serial monitor is below
Your host has some sort of protection (maybe against bots), that expects a _test cookie set by JavaScript, if not present.
You could acquire the cookie, by first visiting the site with the browser and copy paste the cookie into your code.
You would need to do it from the same IP, that your ESP8266 will be introduced to the server, since the cookie is IP bound.
In this case you would have a problem, if you have a dynamic IP and also the longevity of the cookie is unknown.
You could also acquire the cookie by parsing the response, but the cookie is AES encrypted and that would be somewhat complicated.
The most sensible solution would be to switch to a host without such protection.
This is a solution to basically the same problem in this and this question.
I am having some fun playing around with an Arduino (Uno rev 3) and a thermal printer (this model https://www.sparkfun.com/products/10438). The Arduino makes a request every 10 seconds to my local machine (via an Ethernet shield) and stores the response (if 200) on an SD card. It then prints this out using this library https://github.com/adafruit/Adafruit-Thermal-Printer-Library .
So far I have it correctly polling, storing and printing basic text but now I'm trying to use some of the more advanced commands (underline, inverse etc). My ultimate goal is to send images down and handle all of the rendering on the server ala http://printer.gofreerange.com/ .
The problem is that the commands I am sending are been outputted as text characters. Some commands work (line feed), but others are garbled. I have attached both the Arduino code and the basic PHP script it is calling. Any help?
Arduino:
#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>
#include <SoftwareSerial.h>
#include "Adafruit_Thermal.h"
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
const char host[] = "192.168.1.100";
char cacheFilename[] = "TMP";
const byte printer_RX_Pin = 8; // this is the green wire
const byte printer_TX_Pin = 9; // this is the yellow wire
const byte SD_Pin = 4; // the SD Card SPI pin
bool downloadWaiting = false;
bool statusOk = false;
unsigned long content_length = 0;
EthernetClient client;
Adafruit_Thermal printer(printer_RX_Pin, printer_TX_Pin);
void die(unsigned int times) {
while(true);
}
void checkForDownload() {
Serial.println("checkForDownload");
content_length = 0;
statusOk = false;
unsigned long length = 0;
if (SD.exists(cacheFilename)) {
if (!SD.remove(cacheFilename)) {
die(4);
}
}
File cache = SD.open(cacheFilename, FILE_WRITE);
if(client.connect(host, 80)) {
client.println("GET /printer.php HTTP/1.1");
client.print("Host: "); client.println(host);
client.println("User-Agent: arduino-ethernet");
client.println("Connection: close");
client.println();
bool parsingHeader = true;
while(client.connected()) {
while(client.available()) {
if (parsingHeader) {
client.find((char*)"HTTP/1.1 ");
char statusCode[] = "000";
client.readBytes(statusCode, 3);
statusOk = (strcmp(statusCode, "200") == 0);
client.find((char*)"Content-Length: ");
char c;
while (isdigit(c = client.read())) {
content_length = (content_length * 10) + (c - '0');
}
client.find((char*)"\n\r\n");
parsingHeader = false;
} else {
if(length < content_length) {
cache.write((byte)client.read());
length++;
} else {
client.read();
}
}
}
}
client.stop();
cache.seek(0);
if (statusOk && content_length > 0 && (content_length == length) && (content_length == cache.size())) {
downloadWaiting = true;
}
} else {
client.stop();
}
cache.close();
}
void printFromDownload() {
Serial.println("printFromDownload");
File cache = SD.open(cacheFilename);
byte b;
while (content_length--) {
printer.write((byte)cache.read());
}
printer.feed();
cache.close();
downloadWaiting = false;
}
void setup(){
pinMode(SD_Pin, OUTPUT);
if (!SD.begin(SD_Pin)) {
die(2);
}
if (Ethernet.begin(mac) == 0) {
die(3);
}
Serial.begin(9600);
printer.begin(255);
delay(1000);
}
void loop() {
if (downloadWaiting) {
printFromDownload();
delay(5000);
} else {
checkForDownload();
if (!downloadWaiting) {
delay(10000);
}
}
}
PHP:
<?php
ob_start();
// Turn on Inverse mode
// Doesn't work
echo pack('S', 29);
echo pack('S', 66);
echo pack('S', 1);
$string = 'Testing 1, 2, 3';
foreach(str_split($string) as $char) {
echo pack('S', ord($char)); // works
}
// Turn off Inverse mode
echo pack('S', 29);
echo pack('S', 66);
echo pack('S', 0);
// Line feed
echo pack('S', 10); // works
$content = ob_get_clean();
$length = strlen($content);
header("Content-Length: $length");
echo $content;
It seems that you can't print bitmap data directly with printer.write(). The printer expects some special bytes to turn on bitmap printing mode as you can see in the printBitmap() method. (writeBytes(18, 42, chunkHeight, rowBytesClipped))
void Adafruit_Thermal::printBitmap(
int w, int h, const uint8_t *bitmap, bool fromProgMem) {
int rowBytes, rowBytesClipped, rowStart, chunkHeight, x, y, i;
rowBytes = (w + 7) / 8; // Round up to next byte boundary
rowBytesClipped = (rowBytes >= 48) ? 48 : rowBytes; // 384 pixels max width
for(i=rowStart=0; rowStart < h; rowStart += 255) {
// Issue up to 255 rows at a time:
chunkHeight = h - rowStart;
if(chunkHeight > 255) chunkHeight = 255;
writeBytes(18, 42, chunkHeight, rowBytesClipped);
for(y=0; y < chunkHeight; y++) {
for(x=0; x < rowBytesClipped; x++, i++) {
PRINTER_PRINT(fromProgMem ? pgm_read_byte(bitmap + i) : *(bitmap+i));
}
i += rowBytes - rowBytesClipped;
}
timeoutSet(chunkHeight * dotPrintTime);
}
prevByte = '\n';
}
Your sketch will need to understand the data coming from the PHP and know when to send individual characters as bytes with printer.write() and when to send bytes as an image with printer.printBitmap(). This way the printer is receiving the proper commands to prep it for printing the appropriate data. You will need to construct some metadata around what you want to print in PHP and send that to the Arduino. A JSON format might look like this:
{"reciept": [
{
"type": "text",
"style": "bold",
"value": "Thank you for your purchase"
},
{
"type": "bitmap",
"pos": "center",
"value": ".... binary data ..."
}
]}
Now your Arduino sketch will understand when to send bytes individually as text and when to send a lot of data as a bitmap.
A more compact format might use line feeds as a break between segments:
F|bold
T|Thank you for shopping with us\r
P|Center
B|...binary data (with \r escaped)... \r
Or, you can send the amount of data with each segment to avoid escaping binary data much like the Content-Length header of HTTP
F4|boldT32|Thank you for shopping with us\rP6|CenterB3000|...binary data...
I have server in C++ writen with boost.asio and php client. when i send over small amount of data i get all the data but when i send long string i loose most of it.
Here is the part where i send data from my server, it says i have sent out 65536 bytes
void handle_write(const boost::system::error_code& /*error*/,
size_t size/*bytes_transferred*/) {
cout <<size<<endl;
}
void handler_read(const boost::system::error_code&, std::size_t size) {
istream is(&buffer);
string myString;
getline(is, myString);
Manager myManager(myString);
string response = myManager.getResponse();
boost::asio::async_write(socket_,
boost::asio::buffer(response),
boost::bind(&tcp_connection::handle_write, shared_from_this(),
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
Here i make the string i will be sending
string getMap(string name, string pass) {
if (name == "admin" && pass == "123") {
string response = "";
ConvertTypes types;
response = types.intToString(MAP_HEIGHT) + " ";
response += types.intToString(MAP_WIDTH) + "\r\n";
for (int i=0; i<MAP_HEIGHT;i++) {
for (int j=0;j<MAP_WIDTH;j++) {
response += types.intToString(
worldMap[i][j].getHeight()) + " ";
response += types.intToString(
worldMap[i][j].getIsTown()) + " ";
response += string (1, worldMap[i][j].getTetrain())
+"\r\n";
}
}
return response;
} else {
return "";
}
}
On php side i read the sent data, stream_get_meta_data says i only received 8183 bytes of data.
print_r($this->socket->getStatus());
for ($i=0; $i<$MAP_HEIGHT;$i++) {
for ($j=0; $j<$MAP_WIDTH;$j++) {
$this->response = $this->socket->readLine();
$this->response = explode(' ', $this->response);
echo "<p>";
echo "$i $j <br>";
print_r($this->response);
echo '<br>';
print_r($keyArray);
$map[$i][$j] = array_combine($keyArray, $this->response);
$this->response = $this->socket->readLine();
} }
}
You can send one large block via socket, but receiving side might get several blocks of smaller sizes, for example:
send -> 10000 bytes
receive <- 3000 bytes
receive <- 2000 bytes
receive <- 4500 bytes
receive <- 500 bytes
this is only an example, TCP does not guarantee send and receive blocks will be the same size.
I've found a answer. I was sending data from server in unsafe way. When async_write gave up controll to something else rest of the data was lost.
You have to pass string to this class:
class shared_const_buffer {
public:
// Construct from a std::string.
explicit shared_const_buffer(const std::string& data)
: data_(new std::vector<char>(data.begin(), data.end())),
buffer_(boost::asio::buffer(*data_))
{
}
// Implement the ConstBufferSequence requirements.
typedef boost::asio::const_buffer value_type;
typedef const boost::asio::const_buffer* const_iterator;
const boost::asio::const_buffer* begin() const { return &buffer_; }
const boost::asio::const_buffer* end() const { return &buffer_ + 1; }
private:
boost::shared_ptr<std::vector<char> > data_;
boost::asio::const_buffer buffer_;
};
and send this buffer not raw string. That way you don't loose data.