C:发送文件到套接字
我正在尝试使用套接字发送二进制文件。
FILE *file;
char *file_data;
file = fopen(filepath, "rb");
//Allocate memory
file_data=(char *)malloc(fileLen+1);
//Read file contents into buffer
fread(file_data, fileLen, 1, file);
fclose(file);
sent = send(client, file_data, strlen(header)+fileLen, 0);
它工作正常,但有些文件太大,我想读取一部分来缓冲,发送它,然后读取第二部分,发送它等等。
我尝试使用 fread 和 fgets 获取零件,但失败了 =( 如何正确执行?
UPD:问题在于读取来自客户端的传入请求。我没有读它。如果我这样做,不会发生任何不好的事情
I`m trying to send binary file using socket.
FILE *file;
char *file_data;
file = fopen(filepath, "rb");
//Allocate memory
file_data=(char *)malloc(fileLen+1);
//Read file contents into buffer
fread(file_data, fileLen, 1, file);
fclose(file);
sent = send(client, file_data, strlen(header)+fileLen, 0);
It works OK, but some files a too large and I want to read a part to buffer, send it, then read the second part, send it and so on.
I tried to get parts using fread and fgets, but i failed =( How to do it correctly?
UPD: the trouble was in reading incoming request from client. I didnt read it. If i do it, nothing bad happens
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
类似于:
但是您还需要以块的形式发送数据,并且不能假设您将在一次调用中发送所有数据。
因此
send()
必须看起来更像这样:然后您必须准备好在 send() 调用期间处理中断(在这种情况下,您只需要重复从相同的位置):
在 EINTR 的(希望很少见)情况下,发送将在下一次循环迭代中重复,并且可能有机会完成下一次并将数据返回到您的程序
Something like:
But you also need to send the data in chunks, and cannot assume that you'll send all of it in one call.
So that
send()
would have to look more like this:And then you have to be prepared to handle interrupts during the send() call (in which case you'll just need to repeat the send from the same position):
In the (hopefully rare) case of EINTR, the send would be repeated on the next loop iteration, and would probably have chance to complete next time and return data to your program
Fread() 块
Fread() in chunks
如果您只发送一个文件,则不需要标头。您只需在完成发送后关闭 TCP 连接,当客户端收到 EOF 时,他们就会知道它是 EOF ;-)
您可能还会发现这个问题非常有启发性:
为什么写入一个封闭的 TCP 套接字比读取一个更糟糕?
You won't need the header if you're just sending one file. You just close the TCP connection when you're done sending and the when the client receives the EOF then they'll know it's EOF ;-)
You might also find this SO question very illuminating:
Why is writing a closed TCP socket worse than reading one?
问题在于读取来自客户端的传入请求。我没读过。如果我这样做,不会有什么不好的事情发生
the trouble was in reading incoming request from client. I didnt read it. If i do it, nothing bad happens