You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Getting a parse error when parsing data received via a read loop over a tcp socket.
The Error
[json.exception.parse_error.101]
Please describe the steps to reproduce the issue.
Set up a tcp socket and read data from the socket stream with a read loop
Set the buffer size to be smaller than the amount of data coming over the socket so the data has to be read in a loop
json parse the data after it's been read.
I have a string that's 95 chars long, I set my read buffer to 55 to make sure that all data gets read via the loop. I can confirm that I receive all of the data with a std::cout of the buffer, but when I try to parse the buffer, I get a parse error. The parse function thinks it only received 55 chars. Changing the buffer size to a number larger than the message text fixes the issue. But I need to be able to read the data in a loop as I will be transferring large json dumps. My code is below:
voidSocketCommunication::receive_node_message(int sock) {
constint BUFFER_SIZE = 55; // <--- problem is herechar buffer[BUFFER_SIZE] = {0};
...
while (offset < sizetoread) {
ssize_t reader = recv(sock, buffer+offset, sizetoread-offset, 0); // get messageif(reader == 0) {
break;
}
offset+=reader;
}
std::cout << buffer << std::endl; // <--- prints the entire string (all 95 characters) shown below// {"Message":{"Peers":null,"SocketConnector":{"ip":"127.0.0.1","port":10002},"Type":"DISCOVERY"}}auto j = nlohmann::json::parse(buffer); // <--- fails with parse error
What is the expected behavior?
The json should get parsed because I'm providing a full json string to the parse() method.
And what is the actual behavior instead?
libc++abi: terminating with uncaught exception of type nlohmann::detail::parse_error:
[json.exception.parse_error.101] parse error at line 1, column 56: syntax error while parsing
value - invalid string: missing closing quote; last read: '"127.0'
Which compiler and operating system are you using?
Compiler: /usr/bin/g++ -Wall -std=c++17
Operating system: OSX 12.1
Which version of the library did you use?
3.10.5
The text was updated successfully, but these errors were encountered:
Indeed, the library will only read the buffer until its last character, so the error message makes sense here. Writing/reading to the buffer past its size should be undefined behavior - please run your code with Valgrind or ASAN.
I'm not sure I understand your answer (sorry I'm new to cpp) but it would seem that receiving large amounts of data in pieces via a socket read loop is well established doctrine in terms of network programming. Also, I'm giving nlohmann::json a full string to work with. The program reads data into a variable until the loop is done and the entire message has been delivered. Only after I've received the entire message do I try to json parse it. As you can see in the example above, I'm sending the entire string to be parsed.
What is the issue you have?
Getting a parse error when parsing data received via a read loop over a tcp socket.
The Error
[json.exception.parse_error.101]
Please describe the steps to reproduce the issue.
I have a string that's 95 chars long, I set my read buffer to 55 to make sure that all data gets read via the loop. I can confirm that I receive all of the data with a std::cout of the buffer, but when I try to parse the buffer, I get a parse error. The parse function thinks it only received 55 chars. Changing the buffer size to a number larger than the message text fixes the issue. But I need to be able to read the data in a loop as I will be transferring large json dumps. My code is below:
What is the expected behavior?
The json should get parsed because I'm providing a full json string to the parse() method.
And what is the actual behavior instead?
libc++abi: terminating with uncaught exception of type nlohmann::detail::parse_error:
[json.exception.parse_error.101] parse error at line 1, column 56: syntax error while parsing
value - invalid string: missing closing quote; last read: '"127.0'
Which compiler and operating system are you using?
Which version of the library did you use?
3.10.5
The text was updated successfully, but these errors were encountered: