yes, when i did a test of a send of message without the file transfer running perfectly.
Otherwise, I used the return values: the send step from client does not return any error but the read step of server returns the value -1 ?!!
yes, when i did a test of a send of message without the file transfer running perfectly.
Otherwise, I used the return values: the send step from client does not return any error but the read step of server returns the value -1 ?!!
Just so we're clear.
The last part of server.c is
And the last part of client.c isCode:ssize_t result = send(socket,Msg_to_cl,strlen(Msg_to_cl),0); if ( result == -1 ) { perror("Final send failed"); } else { printf("Sent %zd bytes\n", result); }
> if ((socket = envoyer_fich(socket, line_str)) < 0)Code:ssize_t result = read(socket, Msg_from_sr, 100); if ( result == -1 ) { perror("Final read failed"); } else { printf("Received %zd bytes\n", result); }
Why do you modify socket here with the assignment?
What does envoyer_fich actually do?
Why don't you send desc_line (the actual line length) as a parameter to this function?
Are you taking into account that send() and recv() can both be partially successful?
That is, if you send("hello world"), send might return say 7, indicating that "hello w" was sent.
It is YOUR responsibility to call send("orld") to ensure all the data is sent.
> while ((desc_line = getline(&line_str, &line_pos, fichier_cl)) != -1)
Is your JSON reasonably well formatted, or is it some massive file devoid of white-space and newlines?
Not only is this unnecessary, it's actually a bug.Code:if (line_str) { free(line_str); } line_pos = 0;
What will happen on the next call to getline() is that getline will see what it things is a pointer to 0 bytes and try to realloc.
But your pointer is a stale pointer to freed memory, not a valid pointer to zero bytes.
You only need ONE free(line_str), and that's OUTSIDE the while loop.
Also posted here -> Envoyer un message apres le send d'une fichier via socket - C - Programmation - FORUM HardWare.fr
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
yes you're right but the JSON file is sent to the server correctly. I avoided using the socket value as the return value of send_fich but the same problem.
envoyer_fich:
and i have used like this:Code:int envoyer_fich(int socket, char* line) { int desc; desc = send(socket,line, strlen(line), 0); return desc; }
Afterthat, From server: send 19 bytes, from client: received 0 bytesCode:if(envoyer_fich(socket,line_str) < 0) { printf("ERROR: Failed to send file %s.\n", file_name); break; }
Thank you for your response, but i have resolved my problem using a second connection port between server and client