草庐IT

c++ - 通过 boost tcp 传输文件

coder 2023-09-17 原文

我想通过 LAN 通过 tcp 传输一些文件,所以我为我的 TX-Part 编写了以下代码:

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)
    {
    };
};

和接收部分:

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?)

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

最佳答案

UPDATE 在我的回答中我随便说了

I added postfix .received to the output file name to prevent overwriting the source.

我刚刚意识到这可能是您的问题:

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


因此,我修改了代码,使其能够运行。

这是测试主体:

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 的可执行文件对其进行了测试。

完整代码(也是 on Coliru, although Coliru doesn't allow network connections ):

#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();
}

关于c++ - 通过 boost tcp 传输文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22859665/

有关c++ - 通过 boost tcp 传输文件的更多相关文章

  1. ruby - 使用 RubyZip 生成 ZIP 文件时设置压缩级别 - 2

    我有一个Ruby程序,它使用rubyzip压缩XML文件的目录树。gem。我的问题是文件开始变得很重,我想提高压缩级别,因为压缩时间不是问题。我在rubyzipdocumentation中找不到一种为创建的ZIP文件指定压缩级别的方法。有人知道如何更改此设置吗?是否有另一个允许指定压缩级别的Ruby库? 最佳答案 这是我通过查看ruby​​zip内部创建的代码。level=Zlib::BEST_COMPRESSIONZip::ZipOutputStream.open(zip_file)do|zip|Dir.glob("**/*")d

  2. ruby - 其他文件中的 Rake 任务 - 2

    我试图在一个项目中使用rake,如果我把所有东西都放到Rakefile中,它会很大并且很难读取/找到东西,所以我试着将每个命名空间放在lib/rake中它自己的文件中,我添加了这个到我的rake文件的顶部:Dir['#{File.dirname(__FILE__)}/lib/rake/*.rake'].map{|f|requiref}它加载文件没问题,但没有任务。我现在只有一个.rake文件作为测试,名为“servers.rake”,它看起来像这样:namespace:serverdotask:testdoputs"test"endend所以当我运行rakeserver:testid时

  3. ruby-on-rails - 在 Rails 中将文件大小字符串转换为等效千字节 - 2

    我的目标是转换表单输入,例如“100兆字节”或“1GB”,并将其转换为我可以存储在数据库中的文件大小(以千字节为单位)。目前,我有这个:defquota_convert@regex=/([0-9]+)(.*)s/@sizes=%w{kilobytemegabytegigabyte}m=self.quota.match(@regex)if@sizes.include?m[2]eval("self.quota=#{m[1]}.#{m[2]}")endend这有效,但前提是输入是倍数(“gigabytes”,而不是“gigabyte”)并且由于使用了eval看起来疯狂不安全。所以,功能正常,

  4. ruby-on-rails - Rails 3 中的多个路由文件 - 2

    Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题

  5. ruby - 将差异补丁应用于字符串/文件 - 2

    对于具有离线功能的智能手机应用程序,我正在为Xml文件创建单向文本同步。我希望我的服务器将增量/差异(例如GNU差异补丁)发送到目标设备。这是计划:Time=0Server:hasversion_1ofXmlfile(~800kiB)Client:hasversion_1ofXmlfile(~800kiB)Time=1Server:hasversion_1andversion_2ofXmlfile(each~800kiB)computesdeltaoftheseversions(=patch)(~10kiB)sendspatchtoClient(~10kiBtransferred)Cl

  6. ruby - 如何将脚本文件的末尾读取为数据文件(Perl 或任何其他语言) - 2

    我正在寻找执行以下操作的正确语法(在Perl、Shell或Ruby中):#variabletoaccessthedatalinesappendedasafileEND_OF_SCRIPT_MARKERrawdatastartshereanditcontinues. 最佳答案 Perl用__DATA__做这个:#!/usr/bin/perlusestrict;usewarnings;while(){print;}__DATA__Texttoprintgoeshere 关于ruby-如何将脚

  7. ruby - 通过 rvm 升级 ruby​​gems 的问题 - 2

    尝试通过RVM将RubyGems升级到版本1.8.10并出现此错误:$rvmrubygemslatestRemovingoldRubygemsfiles...Installingrubygems-1.8.10forruby-1.9.2-p180...ERROR:Errorrunning'GEM_PATH="/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/ruby-1.9.2-p180@global:/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/rub

  8. ruby - 使用 Vim Rails,您可以创建一个新的迁移文件并一次性打开它吗? - 2

    使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta

  9. ruby-on-rails - 如何优雅地重启 thin + nginx? - 2

    我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server

  10. ruby - 通过 erb 模板输出 ruby​​ 数组 - 2

    我正在使用puppet为ruby​​程序提供一组常量。我需要提供一组主机名,我的程序将对其进行迭代。在我之前使用的bash脚本中,我只是将它作为一个puppet变量hosts=>"host1,host2"我将其提供给bash脚本作为HOSTS=显然这对ruby​​不太适用——我需要它的格式hosts=["host1","host2"]自从phosts和putsmy_array.inspect提供输出["host1","host2"]我希望使用其中之一。不幸的是,我终其一生都无法弄清楚如何让它发挥作用。我尝试了以下各项:我发现某处他们指出我需要在函数调用前放置“function_”……这

随机推荐