Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/152.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 从套接字读取基本文件buf_C++_Sockets_Client - Fatal编程技术网

C++ 从套接字读取基本文件buf

C++ 从套接字读取基本文件buf,c++,sockets,client,C++,Sockets,Client,它是来自客户端的代码(使用) 我使用 char buffer[4096] = ""; n = read(aSocketFD, buffer, 4096); 我的问题是: 我能得到像std::basic\u filebuf这样的响应吗?或者我可以重新提交*? Socket是一个文件处理程序,所以我可以这样做,但是如何做呢?如果您问这个问题是因为您想要好的性能: 在linux上,您可以使用sendfile,这适用于简单文件描述符(fd): 其他一些unix系统也支持此API调用,但

它是来自客户端的代码(使用

我使用

   char buffer[4096] = "";
   n = read(aSocketFD, buffer, 4096);  
我的问题是:
我能得到像std::basic\u filebuf这样的响应吗?或者我可以重新提交*?
Socket是一个文件处理程序,所以我可以这样做,但是如何做呢?

如果您问这个问题是因为您想要好的性能:

在linux上,您可以使用
sendfile
,这适用于简单文件描述符(fd):


其他一些unix系统也支持此API调用,但它不是POSIX的一部分,因此不可移植。

我认为您需要将std::streambuf子类化以实现所需功能。

对于C code,您可以使用类似FILE*calling fdopen()的描述符:将流与文件描述符关联(POSIX.1标准).

我并没有真正解决您的实际问题,但可能值得查看boost.asio之类的网络库。您可能可以通过使用获得
文件
指针,我想我看到有人在做socket“iostreams”,但我记不起在哪里看到过。谷歌是你的朋友。是的,我可以使用std::basic_streambuf。但是我需要基本的_filebuf@user1143825虽然套接字描述符可以由文件处理函数使用,如
读取
写入
关闭
,但它不是文件。因此,您不应使用例如
std::basic_filebuf
,而应直接从
std::basic_streambuf
继承。一个明显的原因是您不需要对seek的支持,因为它在sockets上不起作用。文件*_fd=fdopen(aSocketFD,“r+”);std::基本文件流输入(fd);
   char buffer[4096] = "";
   n = read(aSocketFD, buffer, 4096);  
ssize_t sendfile(int out_fd, int in_fd, off_t *offset, size_t count);