Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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;      /* end */
            else
                return(r);  /* error */
        }
    }
    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;      /* end */
            else
                return(r);  /* error */
        }
    }
    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.
Posted
Updated 23-Jun-23 6:13am
v3
Comments
Richard MacCutchan 22-Jun-23 10:05am    
It may be a problem in the parse_log function, but there is no way anyone here can tell.
jeron1 22-Jun-23 10:06am    
I am not sure I understand your explanation of the issue, but I don't see where you're looking for the "L3 End of record", the point at which I believe you want to process the received information. Also, if your data is received in multiple packets the buffer will probably be overwritten on receipt of the 2nd and 3rd.... packets, though there is not enough detail to know.
Member 15992924 22-Jun-23 12:57pm    
hello I Update in post general datas code and explications thanks. I hope you understand my English, excuse me.
jeron1 22-Jun-23 16:23pm    
Is the data stored in buf a null terminated string?
Member 15992924 23-Jun-23 12:58pm    
It does not end in null, it does not send me the 0 byte.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900