在 C++ 中使用 TCP 从服务器端向客户端发送文件
现在这是一个更多的请求,因为我找不到 任何简单直接的例子。
问题:我想将文件从服务器端发送到客户端。
当服务器已经启动并正在侦听端口并且客户端请求文件时(文件名被接受为带有服务器 IP 地址(例如 127.0.0.1 和端口号)的参数) 然后开始传输过程,直到文件被复制。
另外有人可以告诉我如何测量服务器端的平均传输速度吗?
顺便说一句:我正在运行 Linux x86 干杯, 回声9
Now this is a bit more of a request since I am not able to find
any simple and direct example for this.
Issue: I want to send a file from the server side to the client side.
When the server is already up and listening on a port and the client requests a file (file's name being accepted as a parameter with the server IP address such as 127.0.0.1 and port no.)
and then the process of transfer starts till the file is copied.
Also can someone incorporate how can i measure the average transfer speed on the server side?
BTW: I am running Linux x86
Cheers,
echo9
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
查看Beej 网络编程指南。那里有很多示例展示了如何使用套接字实现客户端/服务器架构并在它们之间发送数据。
编辑:
检查本教程中的第 8 项和第 9 项,了解客户端/服务器上的完整示例。请注意,在第 8 项中,服务器向客户端发送
char*
:在本例中,它是
“Welcome to my server.\n”
字符串,以及下一个参数是要发送的字符串的大小。当您需要从文件发送数据时,情况是一样的:首先,您需要从文件中读取数据并将其存储在通过 malloc 手动分配的
char* 缓冲区;
中()。像这样的东西:
Check Beej's Guide to Network Programming. There are lots of examples there that shows how to implement a client/server architecture using sockets and send data between them.
EDIT:
Check items 8 and 9 from this tutorial for a complete example on client/server. Note that on item 8, the server sends a
char*
to the client:On this case, it's the
"Welcome to my server.\n"
string, and the next parameter is the size of the string you want to send.When you need to send data from a file it's the same thing: first, you need to read the data from the file and store it in a
char* buffer;
that you manually allocated through malloc().Something like this:
这是一个简单的协议:
该协议的优点是能够使用您的 Web 浏览器作为客户端,将您的 Web 服务器作为主机,假设它们都支持 HTTP/0.9。
这是一个客户端。
Here's a simple protocol:
This protocol has the advantage of being able to use your web browser as the client, and your web server as the host, assuming they each support HTTP/0.9.
Here's a client.
您可能需要考虑使用 sendfile(2)< /a>.
You might want to consider using sendfile(2).