Hello, I have a problem with my C code with the parsing of the information received that a 3rd party client-socket sends me.
It is a socket-server with an infinite loop to clean the buffer and always stay active to receive data, I have 2 options to parse this data where I encounter 2 problems depending on the one I use.
1st Option: parsing byte by byte from top to bottom in a plain text file
void parse_log(char msg[])
{
FILE *fp;
fp = fopen("/report/socket.log", "a+");
fprintf(fp, "%s\n\n", msg);
fclose(fp);
fprintf(stderr, "\n%s\n\n", msg);
}
void do_server(int fd)
{
char buf[BUFS];
int i, r=1;
int read_bytes(int, char*, int);
while ((r = read_bytes(fd, &buf[i], 1)) > 0)
parse_log(buf);
write(1, buf, r);
}
int read_bytes(int fd, char *buf, int bytes)
{
int n,r;
n = bytes;
while (n > 0) {
r = read(fd, buf, n);
if(r > 0) {
n = n - r;
buf = buf + r;
}
else {
if(r == 0)
break;
else
return(r);
}
}
return (bytes - n);
}
Advantage: The sending of data that the client-socket makes to my socket-server every 10 seconds is correct and I parse the information from top to bottom in a file.
Problem: To consume this information with Python, what I do is parse the information to a json and what that python script does is read the information from right to left, taking into account the delimiters that separate the information. The problem is how could I place that information with this option 1 in the parse so that it is from right to left, not from top to bottom.
2nd Option: the parsing of the buff plain text.
void parse_log(char msg[])
{
FILE *fp;
fp = fopen("/report/socket.log", "w+");
fprintf(fp, "%s\n\n", msg);
fclose(fp);
fprintf(stderr, "\n%s\n\n", msg);
}
void do_server(int fd)
{
char buf[BUFS];
int i, r=1;
for (i = 0; (i < sizeof(buf)) && (r >0); i++)
r = read_bytes(fd, &buf[i], 1);
parse_log(buf);
err_log("DATOS FROM 3º PARTY RX & PARSE DATA BUFFER TO FILE - OK SOCKET 2!!");
write(1, buf, i);
}
int read_bytes(int fd, char *buf, int bytes)
{
int n,r;
n = bytes;
while (n > 0) {
r = read(fd, buf, n);
if(r > 0) {
n = n - r;
buf = buf + r;
}
else {
if(r == 0)
break;
else
return(r);
}
}
return (bytes - n);
}
Advantage: The information I receive parses from right to left correctly in the text file so that later the python script does the parsing to json correctly.
Problem: When I put this option 2, the client-socket does not send me the data every 10 seconds, it sends me almost every 50 seconds and the client-socket shows a pipe broke error from the socket-server. Another problem with this option 2 apart from the previous one, also the buffer size if I put a little it does not complete the information that it sends me and if I put a lot it doubles the information as if it did not send me the 0 byte to stop.
So I find myself in a problem that I don't know what to do, I'm not an expert in C too much, I'm getting to not be an expert the truth but I've stuck with this.
I hope you understand the explanation I have tried to explain it as best as possible under my knowledge.
Sorry for my english Thank you
What I have tried:
I have tried to increase the buffer or decrease the buffer but nothing without success.
Explanation of what I do:
The problem is described above as the application is issuing the problem.
What it does is create a socket with an infinite loop, when it receives the data the information in raw .log or .txt is parsed to a file
So the problem is that the 3rd party application sends the data every 10 seconds in the direction of the socket server and I am not getting the data every 10 seconds. 30 seconds it establishes communications and after 10 seconds the data arrives.
We are talking almost 40 seconds
If I change everything to be byte-by-byte or just leave a simple socket and print the data only in console I get every 10 seconds. correctly. I think it is well explained both above and the tests carried out.
I receive I have a relevant piece of data that is supposed to be the end of the data set: I get an END_OF_RECORD at the end of the data set, can this value that arrives in the data be treated as the NULL point to close? I don't know if it is an opinion, I provide information that comes to me from data in case it is relevant to be able to fix this problem
I update code in the topic.