Send unspecified amount of JSON strings via socket

2

I have a cliente/servidor application where the client makes a request to the server and it asks a banco de dados and then returns that data to the client in JSON format, as I do not know how many records I have in my I thought of sending each record to a send() , but on the client I'm losing the data of the first send() , and the buffer of the first recv() is storing the data of send()

Código do servidor:

while((row = mysql_fetch_row(res))){
    sprintf(jobj_string, "{ \"nomeUsuario\" : \"%s\", \"idUsuario\" : \"%s\" }", row[1], row[0]);
    if((send(socketNovo, jobj_string, strlen(jobj_string), 0)) == SOCKET_ERROR){
        perror("Falha ao enviar usuario");
        exit(0);
    }
}

Código do cliente:

for(;;){
    if((recv(socketServidor, jobj_string, 300, 0)) == SOCKET_ERROR){
        perror("socket recv");
        exit(0);
    }
    jobj = json_tokener_parse(jobj_string);
    if(jobj != NULL)
        json_object_object_foreach(jobj, key, val){
            /* processa o JSON */
        }
}

what the server says:

{ "nomeUsuario" : "user1", "idUsuario" : "1" }
{ "nomeUsuario" : "user2", "idUsuario" : "2" }
{ "nomeUsuario" : "user3", "idUsuario" : "3" }
{ "nomeUsuario" : "user4", "idUsuario" : "4" }

each line being a send ();

What the client's first recv buffer stores:

: "user2", "idUsuario" : "2" }
{ "nomeUsuario" : "user3", "idUsuario" : "3" }
{ "nomeUsuario" : "user4", "idUsuario" : "4" }

I lost the data of the first send() , and matade of the second, I used wireshark and verified that the server is sending the data correctly, another idea would be to send all the records in only send() , but as I have no way to determine how many records exist in my database, I run the risk of popping the buffer size of the client.

How would you best send the records to the customer without knowing the size?

    
asked by anonymous 02.10.2017 / 14:35

0 answers