在 C++ 中使用 TCP 从服务器端向客户端发送文件

发布于 2024-11-05 06:40:26 字数 233 浏览 0 评论 0原文

现在这是一个更多的请求,因为我找不到 任何简单直接的例子。

问题:我想将文件从服务器端发送到客户端。

当服务器已经启动并正在侦听端口并且客户端请求文件时(文件名被接受为带有服务器 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

怪我入戏太深 2024-11-12 06:40:26

查看Beej 网络编程指南。那里有很多示例展示了如何使用套接字实现客户端/服务器架构并在它们之间发送数据。

编辑

检查本教程中的第 8 项和第 9 项,了解客户端/服务器上的完整示例。请注意,在第 8 项中,服务器向客户端发送 char*

send(fd2,"Welcome to my server.\n",22,0); /* send to the client welcome message */

在本例中,它是 “Welcome to my server.\n” 字符串,以及下一个参数是要发送的字符串的大小。

当您需要从文件发送数据时,情况是一样的:首先,您需要从文件中读取数据并将其存储在通过 malloc 手动分配的 char* 缓冲区; 中()。

像这样的东西:

char* buffer;
buffer = (char*) malloc(1024); // let's say your file has 1KB of data

/* insert here the code to read data from the file and populate buffer with it */

send(fd2, buffer, 1024,0);

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:

send(fd2,"Welcome to my server.\n",22,0); /* send to the client welcome message */

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:

char* buffer;
buffer = (char*) malloc(1024); // let's say your file has 1KB of data

/* insert here the code to read data from the file and populate buffer with it */

send(fd2, buffer, 1024,0);
少女情怀诗 2024-11-12 06:40:26

这是一个简单的协议:

CLIENT                   SERVER
                         socket(), bind(), listen()
socket(), connect()
                         accept()
send("GET filename\n")
                         recv(buffer)
                         inspect buffer, parse filename (stop at space or \n),
                         open() file.
                         sendfile(file, socket)
                         close(socket)
                         close(file)
 recv(socket)
 close()

该协议的优点是能够使用您的 Web 浏览器作为客户端,将您的 Web 服务器作为主机,假设它们都支持 HTTP/0.9

这是一个客户端

Here's a simple protocol:

CLIENT                   SERVER
                         socket(), bind(), listen()
socket(), connect()
                         accept()
send("GET filename\n")
                         recv(buffer)
                         inspect buffer, parse filename (stop at space or \n),
                         open() file.
                         sendfile(file, socket)
                         close(socket)
                         close(file)
 recv(socket)
 close()

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.

反差帅 2024-11-12 06:40:26

您可能需要考虑使用 sendfile(2)< /a>.

You might want to consider using sendfile(2).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文