通过boost tcp进行文件传输

5

我想通过局域网上的TCP传输一些文件,因此我为我的TX部分编写了以下代码:

void send_data(char * filename, char * dest)
{
    try
    {
        boost::asio::io_service io_service;
        char dest_t = *dest;
        std::string adr = ip_adr_display[0] + ':' + boost::lexical_cast<std::string>(PORTNUM_TCP_IN);
        std::cout << "Adress is: " << adr << " and file is: " << filename << '\n';
        if(debugmode)
            debug_global << adr << '\n';
        std::string file = filename;
        async_tcp_client client(io_service, adr, file);
        io_service.run();
    }
    catch(std::exception& e)
    {
    };
};

还有RX-Part:

void rec_data(void)
{
    try
    {
        std::cout << "Receiving data...\n";
        async_tcp_server *recv_file_tcp_server = new async_tcp_server(PORTNUM_TCP_IN);
        if(debugmode)
            debug_global << "Receiving...\n";
        delete recv_file_tcp_server;
    }
    catch(std::exception &e)
    {
    };
};

以下是服务器和客户端代码:

使用以下服务器和客户端代码:

using boost::asio::ip::tcp;
    class async_tcp_client
    {
    public: 
        async_tcp_client(boost::asio::io_service& io_service, const std::string& server, const std::string& path):resolver_(io_service), socket_(io_service)
        {
            size_t pos = server.find(':');
            if(pos==std::string::npos)
                return;
            std::string port_string = server.substr(pos+1);
            std::string server_ip_or_host = server.substr(0,pos);
            source_file.open(path.c_str(), std::ios_base::binary|std::ios_base::ate);
            if(!source_file)
            {
                std::cout << "Failed to open " << path << std::endl;
                return;
            }
            size_t file_size = source_file.tellg();
            source_file.seekg(0);
            std::ostream request_stream(&request_);
            request_stream << path << "\n" << file_size << "\n\n";
            std::cout << "Request size: " << request_.size() << std::endl;
            tcp::resolver::query query(server_ip_or_host, port_string);
            resolver_.async_resolve(query, boost::bind(&async_tcp_client::handle_resolve, this, boost::asio::placeholders::error, boost::asio::placeholders::iterator));
        };
    private:
        void handle_resolve(const boost::system::error_code & err, tcp::resolver::iterator endpoint_iterator)
        {
            if(!err)
            {
                tcp::endpoint endpoint = *endpoint_iterator;
                socket_.async_connect(endpoint, boost::bind(&async_tcp_client::handle_connect, this, boost::asio::placeholders::error, ++endpoint_iterator));
            }
            else
            {
                std::cout << "Error: " << err.message() << '\n';
            }
        };

        void handle_connect(const boost::system::error_code &err, tcp::resolver::iterator endpoint_iterator)
        {
            if(!err)
            {
                boost::asio::async_write(socket_, request_, boost::bind(&async_tcp_client::handle_write_file, this, boost::asio::placeholders::error));
            }
            else if(endpoint_iterator != tcp::resolver::iterator())
            {
                socket_.close();
                tcp::endpoint endpoint = *endpoint_iterator;
                socket_.async_connect(endpoint, boost::bind(&async_tcp_client::handle_connect, this, boost::asio::placeholders::error, ++endpoint_iterator));
            }
            else
            {
                std::cout << "Error: " << err.message() << '\n';
            };
        }

        void handle_write_file(const boost::system::error_code& err)
        {
            if(!err)
            {
                if(source_file.eof() == false)
                {
                    source_file.read(buf.c_array(), (std::streamsize)buf.size());
                    if(source_file.gcount()<= 0)
                    {
                        std::cout << "read file error" << std::endl;
                        return;
                    };
                    std::cout << "Send " << source_file.gcount() << "bytes, total: " << source_file.tellg() << " bytes.\n";
                    boost::asio::async_write(socket_, boost::asio::buffer(buf.c_array(), source_file.gcount()),boost::bind(&async_tcp_client::handle_write_file, this, boost::asio::placeholders::error));
                    if(err)
                    {
                        std::cout << "Send error: " << err << std::endl;
                        return;
                    }
                }
                else
                    return;
            }
            else
            {
                std::cout << "Error: " << err.message() << "\n";
            }
        };


        tcp::resolver resolver_;
        tcp::socket socket_;
        boost::array<char, 1024> buf;
        boost::asio::streambuf request_;
        std::ifstream source_file;
    };

    class async_tcp_connection: public boost::enable_shared_from_this<async_tcp_connection>
    {
    public:
        async_tcp_connection(boost::asio::io_service& io_service):socket_(io_service), file_size(0){}
        void start()
        {
            if(debugmode)
                debug_global << __FUNCTION__ << std::endl;
            async_read_until(socket_, request_buf, "\n\n", boost::bind(&async_tcp_connection::handle_read_request, shared_from_this(), boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
        }
        boost::asio::ip::tcp::socket& socket(){return socket_;}
    private:
        boost::asio::streambuf request_buf;
        size_t file_size;
        std::ofstream output_file;
        boost::asio::ip::tcp::socket socket_;
        boost::array<char, 40960> buf;
        void handle_read_request(const boost::system::error_code& err, std::size_t bytes_transferred)
        {
            if(err)
            {
                return handle_error(__FUNCTION__, err);
            }
            if(debugmode)
                debug_global << __FUNCTION__ << "(" << bytes_transferred << ")" <<", in_avail = " << request_buf.in_avail() << ", size = " << request_buf.size() << ", max_size = " << request_buf.max_size() << ".\n";
            std::istream request_stream(&request_buf);
            std::string file_path;
            request_stream >> file_path;
            request_stream >> file_size;
            request_stream.read(buf.c_array(), 2);
            if(debugmode)
                debug_global << file_path << " size is " << file_size << ", tellg = " << request_stream.tellg() << std::endl;
            size_t pos = file_path.find_last_of('\\');
            if(pos!= std::string::npos)
                file_path = file_path.substr(pos+1);
            output_file.open(file_path.c_str(), std::ios_base::binary);
            if(!output_file)
            {
                if(debugmode)
                    debug_global << "Failed to open: " << file_path << std::endl;
                return;
            }
            do{
                request_stream.read(buf.c_array(), (std::streamsize)buf.size());
                if(debugmode)
                    debug_global << __FUNCTION__ << " write " << request_stream.gcount() << " bytes.\n";
                output_file.write(buf.c_array(), request_stream.gcount());
            }while(request_stream.gcount() > 0);
            async_read(socket_, boost::asio::buffer(buf.c_array(), buf.size()),boost::bind(&async_tcp_connection::handle_read_file_content, shared_from_this(), boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
        }

        void handle_read_file_content(const boost::system::error_code& err, std::size_t bytes_transferred)
        {
            if (bytes_transferred>0)
            {
                output_file.write(buf.c_array(), (std::streamsize)bytes_transferred);
                if(debugmode)
                    debug_global << __FUNCTION__ << " recv " << output_file.tellp() << " bytes."<< std::endl;
                if (output_file.tellp()>=(std::streamsize)file_size)
                {
                    return;
                }
            }
            if (err)
            {
                return handle_error(__FUNCTION__, err);
            }
            async_read(socket_, boost::asio::buffer(buf.c_array(), buf.size()), boost::bind(&async_tcp_connection::handle_read_file_content, shared_from_this(), boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
        }

        void handle_error(const std::string& function_name, const boost::system::error_code& err)
        {
            if(debugmode)
                debug_global << __FUNCTION__ << " in " << function_name <<" due to " << err <<" " << err.message()<< std::endl;
        }
    }; 

    class async_tcp_server : private boost::noncopyable
    {
    public:
        typedef boost::shared_ptr<async_tcp_connection> ptr_async_tcp_connection;

        async_tcp_server(unsigned short port):acceptor_(io_service_, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), port), true)
        {
                ptr_async_tcp_connection new_connection_(new async_tcp_connection(io_service_));
                acceptor_.async_accept(new_connection_->socket(), boost::bind(&async_tcp_server::handle_accept, this,new_connection_, boost::asio::placeholders::error));
                io_service_.run();
        }   
        void handle_accept(ptr_async_tcp_connection current_connection, const boost::system::error_code& e)
        {
            if(debugmode)
                debug_global << __FUNCTION__ << " " << e << ", " << e.message()<<std::endl;
            if (!e)
            {
                current_connection->start();
                //ptr_async_tcp_connection new_connection_(new async_tcp_connection(io_service_));
                //acceptor_.async_accept(new_connection_->socket(),
                //    boost::bind(&async_tcp_server::handle_accept, this,new_connection_,
                //    boost::asio::placeholders::error));
            }
        }

        ~async_tcp_server()
        {
            io_service_.stop();
        }
    private:
        boost::asio::io_service io_service_;
        boost::asio::ip::tcp::acceptor acceptor_;
    }; 

如果我想传输一个文件,我必须输入绝对路径(为什么?),如果我输入相对路径(例如 "Image.jpg"),我会收到错误消息“无法打开 Image.jpg”。在成功调用函数后,我得到以下输出:

Adress is: <ip>:<port> and file is: <full file path>
Request size: 91
Send 1024 bytes, total: 1024 bytes
Send 1024 bytes, total: 2048 bytes
etc..
Send 1024 bytes, total: 20480 bytes
Send 406 bytes, total: -1 bytes (Why?)

在接收方,我没有收到任何数据。为什么?我不明白为什么我的代码不起作用...
非常感谢!


相对路径是相对于当前工作目录的...所以你的解决方案就在这里:运行应用程序时检查工作目录。 - sehe
根据GetModuleFileName,这是我放置图像的当前目录。 - arc_lupus
1
GetModuleFilename 可以获取到包含指定模块的文件的完整路径。而当前工作目录是另一回事。无论如何,请查看我的答案。 - sehe
@sehe:好的,我的错误在于当前运行目录并不是我之前认为的exe保存的位置,谢谢! - arc_lupus
这只是问题的一部分。如果您在不同的工作目录中运行发送方和接收方,则可能会有关联。请参阅我的答案。 - sehe
1个回答

6

更新:在我的答案中,我随意地说:

我在输出文件名中添加了后缀.received以防止覆盖源文件。

我突然意识到这可能是你的问题:

如果您将代码与接收器一起使用在发送者相同的机器上,您在仍在发送文件时覆盖源文件......糟糕。


因此,我修改了代码,以便可以运行它。

这是测试主函数:

int main()
{
    boost::thread_group g;
    g.create_thread(rec_data); // get the receiver running

    boost::this_thread::sleep_for(boost::chrono::milliseconds(100));
    g.create_thread([] { send_data("test.cpp"); });

    g.join_all();
}

我在输出文件名后添加了后缀.received,以防止覆盖源文件。运行时,它似乎工作得相当不错:
g++ -std=c++11 -Wall -pedantic -pthread test.cpp -lboost_system -lboost_thread 
./a.out 
md5sum test.cpp test.cpp.received

我们得到了输出。

0dc16e7f0dc23cb9fce100d825852621  test.cpp.received
0dc16e7f0dc23cb9fce100d825852621  test.cpp

我已经测试过它可以处理png和93MB的可执行文件。
完整代码(也可以在Coliru上查看,尽管Coliru不允许网络连接):
#include <boost/asio.hpp>
#include <boost/array.hpp>
#include <boost/bind.hpp>
#include <boost/thread.hpp>

#include <iostream>
#include <fstream>
#include <boost/enable_shared_from_this.hpp>

using boost::asio::ip::tcp;

static bool debugmode = true;
static boost::mutex debug_mutex;
static std::ostream debug_global(std::clog.rdbuf());

class async_tcp_client
{
public:
    async_tcp_client(boost::asio::io_service& io_service, const std::string& server, const std::string& path)
        : resolver_(io_service), socket_(io_service)
    {
        size_t pos = server.find(':');
        if(pos==std::string::npos)
        {
            return;
        }
        std::string port_string = server.substr(pos+1);
        std::string server_ip_or_host = server.substr(0,pos);
        source_file.open(path, std::ios_base::binary|std::ios_base::ate);
        if(!source_file)
        {
            boost::mutex::scoped_lock lk(debug_mutex);
            std::cout << __LINE__ << "Failed to open " << path << std::endl;
            return;
        }
        size_t file_size = source_file.tellg();
        source_file.seekg(0);
        std::ostream request_stream(&request_);
        request_stream << path << "\n" << file_size << "\n\n";
        {
            boost::mutex::scoped_lock lk(debug_mutex);
            std::cout << "Request size: " << request_.size() << std::endl;
        }
        tcp::resolver::query query(server_ip_or_host, port_string);
        resolver_.async_resolve(query, boost::bind(&async_tcp_client::handle_resolve, this, boost::asio::placeholders::error, boost::asio::placeholders::iterator));
    };
private:
    void handle_resolve(const boost::system::error_code & err, tcp::resolver::iterator endpoint_iterator)
    {
        if(!err)
        {
            tcp::endpoint endpoint = *endpoint_iterator;
            socket_.async_connect(endpoint, boost::bind(&async_tcp_client::handle_connect, this, boost::asio::placeholders::error, ++endpoint_iterator));
        }
        else
        {
            boost::mutex::scoped_lock lk(debug_mutex);
            std::cout << "Error: " << err.message() << '\n';
        }
    };

    void handle_connect(const boost::system::error_code &err, tcp::resolver::iterator endpoint_iterator)
    {
        if(!err)
        {
            boost::asio::async_write(socket_, request_, boost::bind(&async_tcp_client::handle_write_file, this, boost::asio::placeholders::error));
        }
        else if(endpoint_iterator != tcp::resolver::iterator())
        {
            socket_.close();
            tcp::endpoint endpoint = *endpoint_iterator;
            socket_.async_connect(endpoint, boost::bind(&async_tcp_client::handle_connect, this, boost::asio::placeholders::error, ++endpoint_iterator));
        }
        else
        {
            boost::mutex::scoped_lock lk(debug_mutex);
            std::cout << "Error: " << err.message() << '\n';
        };
    }

    void handle_write_file(const boost::system::error_code& err)
    {
        if(!err)
        {
            if(source_file)
            //if(source_file.eof() == false)
            {
                source_file.read(buf.c_array(), (std::streamsize)buf.size());
                if(source_file.gcount()<= 0)
                {
                    boost::mutex::scoped_lock lk(debug_mutex);
                    std::cout << "read file error" << std::endl;
                    return;
                };
                {
                    boost::mutex::scoped_lock lk(debug_mutex);
                    std::cout << "Send " << source_file.gcount() << "bytes, total: " << source_file.tellg() << " bytes.\n";
                }
                boost::asio::async_write(socket_, boost::asio::buffer(buf.c_array(), source_file.gcount()),boost::bind(&async_tcp_client::handle_write_file, this, boost::asio::placeholders::error));
            }
            else
            {
                return;
            }
        }
        else
        {
            boost::mutex::scoped_lock lk(debug_mutex);
            std::cout << "Error: " << err.message() << "\n";
        }
    };


    tcp::resolver resolver_;
    tcp::socket socket_;
    boost::array<char, 1024> buf;
    boost::asio::streambuf request_;
    std::ifstream source_file;
};

class async_tcp_connection: public boost::enable_shared_from_this<async_tcp_connection>
{
public:
    async_tcp_connection(boost::asio::io_service& io_service)
        : socket_(io_service), file_size(0)
    {
    }
    void start()
    {
        if(debugmode)
        {
            boost::mutex::scoped_lock lk(debug_mutex);
            debug_global << __FUNCTION__ << std::endl;
        }
        async_read_until(socket_, request_buf, "\n\n", boost::bind(&async_tcp_connection::handle_read_request, shared_from_this(), boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
    }
    boost::asio::ip::tcp::socket& socket()
    {
        return socket_;
    }
private:
    boost::asio::streambuf request_buf;
    std::ofstream output_file;
    boost::asio::ip::tcp::socket socket_;
    size_t file_size;
    boost::array<char, 40960> buf;
    void handle_read_request(const boost::system::error_code& err, std::size_t bytes_transferred)
    {
        if(err)
        {
            return handle_error(__FUNCTION__, err);
        }
        if(debugmode)
        {
            boost::mutex::scoped_lock lk(debug_mutex);
            debug_global << __FUNCTION__ << "(" << bytes_transferred << ")" <<", in_avail = " << request_buf.in_avail() << ", size = " << request_buf.size() << ", max_size = " << request_buf.max_size() << ".\n";
        }
        std::istream request_stream(&request_buf);
        std::string file_path;
        request_stream >> file_path;
        request_stream >> file_size;
        request_stream.read(buf.c_array(), 2);
        if(debugmode)
        {
            boost::mutex::scoped_lock lk(debug_mutex);
            debug_global << file_path << " size is " << file_size << ", tellg = " << request_stream.tellg() << std::endl;
        }
        size_t pos = file_path.find_last_of('\\');
        if(pos!= std::string::npos)
        {
            file_path = file_path.substr(pos+1);
        }
        output_file.open(file_path + ".received", std::ios_base::binary);
        if(!output_file)
        {
            if(debugmode)
            {
                boost::mutex::scoped_lock lk(debug_mutex);
                debug_global << __LINE__ << "Failed to open: " << file_path << std::endl;
            }
            return;
        }
        do
        {
            request_stream.read(buf.c_array(), (std::streamsize)buf.size());
            if(debugmode)
            {
                boost::mutex::scoped_lock lk(debug_mutex);
                debug_global << __FUNCTION__ << " write " << request_stream.gcount() << " bytes.\n";
            }
            output_file.write(buf.c_array(), request_stream.gcount());
        }
        while(request_stream.gcount() > 0);
        async_read(socket_, boost::asio::buffer(buf.c_array(), buf.size()),boost::bind(&async_tcp_connection::handle_read_file_content, shared_from_this(), boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
    }

    void handle_read_file_content(const boost::system::error_code& err, std::size_t bytes_transferred)
    {
        if (bytes_transferred>0)
        {
            output_file.write(buf.c_array(), (std::streamsize)bytes_transferred);
            if(debugmode)
            {
                boost::mutex::scoped_lock lk(debug_mutex);
                debug_global << __FUNCTION__ << " recv " << output_file.tellp() << " bytes."<< std::endl;
            }
            if (output_file.tellp()>=(std::streamsize)file_size)
            {
                return;
            }
        }
        if (err)
        {
            return handle_error(__FUNCTION__, err);
        }
        async_read(socket_, boost::asio::buffer(buf.c_array(), buf.size()), boost::bind(&async_tcp_connection::handle_read_file_content, shared_from_this(), boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
    }

    void handle_error(const std::string& function_name, const boost::system::error_code& err)
    {
        if(debugmode)
        {
            boost::mutex::scoped_lock lk(debug_mutex);
            debug_global << __FUNCTION__ << " in " << function_name <<" due to " << err <<" " << err.message()<< std::endl;
        }
    }
};

class async_tcp_server : private boost::noncopyable
{
public:
    typedef boost::shared_ptr<async_tcp_connection> ptr_async_tcp_connection;

    async_tcp_server(unsigned short port):acceptor_(io_service_, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), port), true)
    {
        ptr_async_tcp_connection new_connection_(new async_tcp_connection(io_service_));
        acceptor_.async_accept(new_connection_->socket(), boost::bind(&async_tcp_server::handle_accept, this,new_connection_, boost::asio::placeholders::error));
        io_service_.run();
    }
    void handle_accept(ptr_async_tcp_connection current_connection, const boost::system::error_code& e)
    {
        if(debugmode)
        {
            boost::mutex::scoped_lock lk(debug_mutex);
            debug_global << __FUNCTION__ << " " << e << ", " << e.message()<<std::endl;
        }
        if (!e)
        {
            current_connection->start();
        }
    }

    ~async_tcp_server()
    {
        io_service_.stop();
    }
private:
    boost::asio::io_service io_service_;
    boost::asio::ip::tcp::acceptor acceptor_;
};

void send_data(std::string const& filename, std::string const& adr = "localhost:6767")
{
    try
    {
        boost::asio::io_service io_service;
        {
            boost::mutex::scoped_lock lk(debug_mutex);
            std::cout << "Adress is: " << adr << " and file is: " << filename << '\n';
        }

        if(debugmode)
        {
            boost::mutex::scoped_lock lk(debug_mutex);
            debug_global << adr << '\n';
        }

        async_tcp_client client(io_service, adr, filename);
        io_service.run();
    }
    catch(std::exception const& e)
    {
        std::cerr << "Exception in " << __PRETTY_FUNCTION__ << ": " << e.what() << "\n";
    };
};

void rec_data(void)
{
    try
    {
        {
            boost::mutex::scoped_lock lk(debug_mutex);
            std::cout << "Receiving data...\n";
        }
        async_tcp_server recv_file_tcp_server(6767);
        if(debugmode)
        {
            boost::mutex::scoped_lock lk(debug_mutex);
            debug_global << "Received\n";
        }
    }
    catch(std::exception const& e)
    {
        std::cerr << "Exception in " << __PRETTY_FUNCTION__ << ": " << e.what() << "\n";
    };
};

int main()
{
    boost::thread_group g;
    g.create_thread(rec_data); // get the receiver running

    boost::this_thread::sleep_for(boost::chrono::milliseconds(100));
    g.create_thread([] { send_data("main.cpp"); });

    g.join_all();
}

2
我在远程机器上启动了接收器,因此没有覆盖它(就像我想的那样)。但我认为我的主要问题是,我通过PSExec远程启动了接收器,认为exe完全在远程计算机上运行,这是错误的。当我在另一台计算机上手动启动exe时,一切都正常... - arc_lupus
@arc_lupus 好吧,这 确实 让它变得更加有趣。我希望你没有覆盖任何重要文件 :) - sehe
我不这么认为,如果没有明确写入,我怎么能覆盖这些文件? - arc_lupus
@LightnessRacesinOrbit:不,据我所知,我需要管理员特权才能删除系统文件... - arc_lupus
1
@arc_lupus 事情是这样的,你不小心覆盖了那些明显要传输到另一台机器上的文件。希望你还没有用它来备份重要文件 :) - sehe

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接