AIexMoran
(Khomin Oleksandr)
June 30, 2020, 5:23pm
1
Hello. I’m trying to transfer a file over the network, but for some reason not all bytes reach the client.
The write function sends the number of bytes needed, but the accept function takes fewer bytes than necessary. What could be the problem?
send file
gchar *contents = NULL;
gsize bytes = 0;
g_file_load_contents(file, NULL, &contents, &bytes, NULL, NULL);
g_output_stream_write_all(out, contents, size, NULL, NULL, NULL);
recv file
while ((read = g_input_stream_read(in, buf, sizeof(buf), NULL, //sizeof(buf) == 65536
NULL)) > 0) {
bytes += read;
if (bytes > size)
break;
if (g_output_stream_write(G_OUTPUT_STREAM(out), buf, read, NULL, // writing to file
NULL) < 0) {
break;
}
}
here is full server-side file https://github.com/unitucode/uchat/blob/server/server/src/client_handler/api/up_down_load_file/download_file.c
and full client-side file
https://github.com/unitucode/uchat/blob/server/client/src/api/up_down_load_file/file_read.c
zbrown
(Zander Brown)
June 30, 2020, 5:50pm
2
bytes += read;
if (bytes > size)
break;
You probably want to do that after g_output_stream_write
AIexMoran
(Khomin Oleksandr)
June 30, 2020, 5:52pm
3
No, size == size of file. So if received bytes bigger than size, I must break the cycle. Or I’m wrong?
AIexMoran
(Khomin Oleksandr)
June 30, 2020, 5:53pm
4
In addition, I noticed that the client does not read the first 3900-4000 bytes.
zbrown
(Zander Brown)
June 30, 2020, 6:05pm
5
Well yes, but you still want to write the final chunk
It helpful though to have
Otherwise it’s hard to give useful answers (since we don’t really know what the problem is )
g_file_load_contents(file, NULL, &contents, &bytes, NULL, NULL);
g_output_stream_write_all(out, contents, size, NULL, NULL, NULL);
I notice here your mixing “bytes” and “size”, but that probably isn’t the cause of your problems
I don’t see you checking GErrors anywhere - again probably not the problem here but could be useful
You could probably use g_file_read
/g_output_stream_splice
actually
That said skimming your code I’m not immediately seeing a smoking gun
1 Like
AIexMoran
(Khomin Oleksandr)
June 30, 2020, 6:26pm
6
g_print("splice = %ld\n", g_output_stream_splice(G_OUTPUT_STREAM(out), in, G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET, NULL, NULL));
On server side it returns 461348, and on client side 457355
AIexMoran
(Khomin Oleksandr)
June 30, 2020, 6:28pm
7
Maybe this has something to do with g_data_input_stream_read_line_finish. Before these, I use this connection with GDataInputStream
AIexMoran
(Khomin Oleksandr)
July 1, 2020, 10:17am
8
If I don’t use
gchar *msg = g_data_input_stream_read_line_finish(
G_DATA_INPUT_STREAM(source_object), res, NULL, NULL);
It works good, but why?
AIexMoran
(Khomin Oleksandr)
July 1, 2020, 10:39am
9
How the file transfer occurs.
Sending size to client via GDataOutputStream
// sleep(2);
Sending a file byte by byte
If you put sleep between these actions, it will work well. But why???
greg-king
(Greg King)
July 1, 2020, 11:50am
10
How the file reception should occur:
Get the size and the name.
Open the file.
Tell the sender whether or not the file could be opened.
Hear the file’s content.
Write that content.
Close the file.
You don’t do step 3. Your sender didn’t give the receiver enough time to do step 2. Probably, by the time step 2 had finished, almost 4000 bytes already had been sent and lost.
2 Likes
AIexMoran
(Khomin Oleksandr)
July 1, 2020, 11:55am
11
Thanks for the correct answer. Thank you all for your help!
system
(system)
Closed
July 15, 2020, 11:55am
12
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.