I'm trying to send sensor data (double type) from client to server via TCP/IP (Winsock). In theory, after connected successfully, we will execute the command send().
I use 2 variables: stringstream ss and string res to store sensor data and convert double type into char type. But when I sent data, on the server showed: Date Time Current Voltage Air-Pressure 000000000000000000000000000000000000000… All data are 0.
I tried other methods but server only received empty or incorrect values. Please suggest me how to fix this problem. Here's my code:
char Data_Send[10000];
char item[] = " Date\t" "Time\t" "Current\t" "Voltage\t" "Air-Pressure\t";
string res;
stringstream ss;
strcpy(Data_Send, item);
for (int32 i = 0; i < returnedCount; i = i + channelCount)
{
ss << userDataBuffer[i]; // Sensor data
ss >> res;
strcat(Data_Send, (char*)&res);
}
if (connect(socketClient, (sockaddr*)&clientAddr, sizeof(clientAddr)) == 0)
{
cout << "\nConnection success..!";
cout << "\nConnect with Server's IP: " << inet_ntoa(clientAddr.sin_addr);
cout << "\nStart communicating";
cin.getline(Data_Send, 5120);
send(socketClient, (char*)&Data_Send, sizeof(Data_Send), 0);
}
c_str
, not&res
- it gets pointer of string instance not pointer to its buffer, sostrcat(Data_Send, (char*)&res);
should bestrcat(Data_Send, res.c_str());
– rafix07sizeof
, when sending. And error-check yoursend()
call. – user207421cin.getline(Data_Send, 5120);
beforesend
?getline
will overwrite entered data inData_Send
. Try sending withoutgetline
. Alsosend(socketClient, (char*)Data_Send, strlen(Data_Send), 0);
– rafix07