lrl*_*eon 0 c++ sockets tcp poco-libraries
我必须要说的第一件事是,这篇文章的灵感来自[Remy Lebeau] [1] /sf/users/4610441/的回答,这里的问题是:
我正在使用poco库和C ++。
目的是通过互联网发送一些大的二进制文件。因此,为了完成此操作,我使用以下消息头:
class FileDesc
{
size_t file_size = 0;
size_t chunk_size = 0;
size_t file_name_len = 0;
char file_name[0];
public:
FileDesc(size_t file_sz, size_t buf_sz)
: file_size(ByteOrder::toNetwork(file_sz)),
chunk_size(ByteOrder::toNetwork(buf_sz)) {}
size_t get_file_size() const { return ByteOrder::fromNetwork(file_size); }
size_t get_chunk_size() const { return ByteOrder::fromNetwork(chunk_size); }
string get_file_name() const
{
const auto len = get_file_name_len();
string ret; ret.reserve(len + 1);
for (size_t i = 0; i < len; ++i)
ret.push_back(file_name[i]);
return ret;
}
size_t get_file_name_len() const
{
return ByteOrder::fromNetwork(file_name_len);
}
size_t size() const
{
return sizeof(*this) + ByteOrder::fromNetwork(file_name_len);
}
static shared_ptr<FileDesc> allocate(size_t file_sz, size_t buf_sz,
const string & name)
{
const auto n = name.size();
const size_t sz = sizeof(FileDesc) + n;
shared_ptr<FileDesc> ret((FileDesc*) malloc(sz), free);
new (ret.get()) FileDesc(file_sz, buf_sz);
memcpy(ret->file_name, name.data(), n);
ret->file_name_len = ByteOrder::toNetwork(n);
return ret;
}
};
Run Code Online (Sandbox Code Playgroud)
该文件将以固定长度的块(可能不是最后一个)发送。该标题在传输开始时发送。发送方部分通知接收方部分,file_size将按大小块发送字节文件,chunk_size并且文件名包含file_name_len字符。在此消息的末尾,通过char数组将文件名填充起来file_name,其长度是可变的,并取决于文件名的长度。静态方法allocate负责分配具有足以存储完整文件名的大小的此标头。
这是发送方部分的代码:
bool send_file(StreamSocket & sock, const string & file_name, long buf_size)
{
stringstream s;
ifstream file(file_name, ios::binary | ios::ate);
if (not file.is_open() or not file.good()) // verify file reading
{
s << "cannot open file " << file_name;
throw domain_error(s.str());
}
long file_size = file.tellg();
if (file_size == 0)
{
s << "file " << file_name << " has zero bytes";
throw domain_error(s.str());
}
file.seekg(0);
unique_ptr<char[]> chunk(new char[buf_size]);
auto desc_ptr = FileDesc::allocate(file_size, buf_size, file_name);
// send the file description
int status = sock.sendBytes(desc_ptr.get(), desc_ptr->size());
if (status != desc_ptr->size())
return false;
do // now send the file
{
long num_bytes = min(file_size, buf_size);
file.read(chunk.get(), num_bytes);
char * pbuf = chunk.get();
auto n = num_bytes;
while (n > 0)
{
status = sock.sendBytes(pbuf, n);
pbuf += status;
n -= status;
}
file_size -= num_bytes;
}
while (file_size > 0);
char ok; // now I wait for receiving part notifies reception
status = sock.receiveBytes(&ok, sizeof(ok));
return ok == 0;
}
Run Code Online (Sandbox Code Playgroud)
send_file(sock, file_name, buf_size)打开文件file_name并通过poco套接字sock按大小块发送其内容buf_len。因此它将执行file_size/buf_len传输以及文件描述符的初始传输。
接收方实例化poco ServerSocket如下:
ServerSocket server_socket(addr);
auto sock = server_socket.acceptConnection();
auto p = receive_file(sock, 256);
Run Code Online (Sandbox Code Playgroud)
receive_file(sock, 256)表示在套接字处sock将收到一个文件,该文件的最大文件长度为256个字节(对于我的应用程序而言已足够)。该例程(是的计数器部分send_file())保存文件,并按以下方式实现:
pair<bool, string>
receive_file(StreamSocket & sock, size_t max_file_name_len)
{
stringstream s;
size_t buf_sz = sizeof(FileDesc) + max_file_name_len;
unique_ptr<char[]> buffer(new char[buf_sz]);
int num = sock.receiveBytes(buffer.get(), buf_sz);
FileDesc * msg = reinterpret_cast<FileDesc*>(buffer.get());
if (msg->size() > num)
return make_pair(false, "");
long chunk_size = msg->get_chunk_size();
if (chunk_size == 0)
throw domain_error("chunk size is zero");
const string file_name = msg->get_file_name();
ofstream file(file_name, ios::binary);
if (not file.is_open() or not file.good())
{
s << "cannot create file" << file_name;
throw domain_error(s.str());
}
long file_size = msg->get_file_size();
unique_ptr<char[]> chunk(new char[chunk_size]);
do
{ // num_bytes is the quantity that I must receive for this chunk
auto num_bytes = min(file_size, chunk_size);
char * pbuf = chunk.get();
long n = num_bytes;
while (n > 0)
{
num = sock.receiveBytes(pbuf, n, 0); // <-- here is potentially the problem
if (num == 0) // connection closed?
return make_pair(file_size == 0, file_name);
pbuf += num;
n -= num;
}
file.write(chunk.get(), num_bytes);
file_size -= num_bytes;
}
while (file_size > 0);
char ok;
num = sock.sendBytes(&ok, sizeof(ok)); // notify reception to sender
return make_pair(true, file_name);
}
Run Code Online (Sandbox Code Playgroud)
receive_file()返回pair<bool,string>这里first表示我的接收正确完成和second文件名。
由于某种原因,我有时无法理解,因此,当num = sock.receiveBytes(pbuf, n, 0)接收到的字节数少于预期的字节n数时,循环便完成了读取其余部分的工作,只是最后一次接收对应于最后一个发送块。
奇怪的是,直到现在,我的问题仅在使用环回(127.0.0.1)时才会发生,这对于测试非常实用,因为我在计算机上进行了全部测试。相反,当在两个不同的对等点之间进行传输时,所有功能都可以正常工作,但这并不表示所有信息都是正确的。
因此,如果有人可以帮助我批评实施并最终指出问题出在哪里,我将不胜感激。
如果您不希望I / O阻塞,则应将socket设置为non-blocking模式。
但是,看起来一件简单的事情过于复杂。这是一个简单的例子:
$ fallocate -l 1G test.in
$ ls -al test.in
-rw-r--r-- 1 alex alex 1073741824 Oct 15 23:31 test.in
$ ls -al test.out
ls: cannot access test.out: No such file or directory
$./download
$ ls -al test.out
-rw-rw-r-- 1 alex alex 1073741824 Oct 15 23:32 test.out
$ diff test.out test.in
$
Run Code Online (Sandbox Code Playgroud)
因此,我们很好-文件是1G并且相同(尽管很无聊)。
客户代码:
$ fallocate -l 1G test.in
$ ls -al test.in
-rw-r--r-- 1 alex alex 1073741824 Oct 15 23:31 test.in
$ ls -al test.out
ls: cannot access test.out: No such file or directory
$./download
$ ls -al test.out
-rw-rw-r-- 1 alex alex 1073741824 Oct 15 23:32 test.out
$ diff test.out test.in
$
Run Code Online (Sandbox Code Playgroud)
这是服务器:
#include "Poco/StreamCopier.h"
#include "Poco/FileStream.h"
#include "Poco/Net/SocketAddress.h"
#include "Poco/Net/ServerSocket.h"
#include "Poco/Net/StreamSocket.h"
#include "Poco/Net/SocketStream.h"
using namespace Poco;
using namespace Poco::Net;
int main()
{
StreamSocket ss;
ss.connect(SocketAddress("localhost", 9999));
SocketStream istr(ss);
std::string file("test.out");
Poco::FileOutputStream ostr(file, std::ios::binary);
StreamCopier::copyStream(istr, ostr);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
享受POCO!
| 归档时间: |
|
| 查看次数: |
2701 次 |
| 最近记录: |