从boost asio客户端发送数据

时间:2016-11-10 10:44:16

标签: c++ boost boost-asio

我正在尝试编写一个程序,它可以通过网络将数据发送到我的Java服务器上侦听端口。

#include "stdafx.h"
#include <boost/asio.hpp>
#include <boost/array.hpp>
#include <iostream>

void do_somenetwork(std::string host, int port, std::string message)
{
    std::array<char, 1024> _arr;
    boost::asio::io_service ios;
    boost::asio::ip::tcp::endpoint endpoint(boost::asio::ip::address::from_string(host), port);
    boost::asio::ip::tcp::socket socket(ios);
    socket.connect(endpoint);

    boost::array<char, 128> buf;
    std::copy(message.begin(), message.end(), buf.begin());
    boost::system::error_code error;

    socket.write_some(boost::asio::buffer(buf, message.size()), error);

    socket.read_some(boost::asio::buffer(_arr));
    std::cout.write(&_arr[0], 1024);

    socket.close();
}

int main(){
    do_somenetwork(std::string("127.0.0.1"), 50000, ";llsdfsdf");

    char ch;
    std::cin >> ch;
}

当我使用这个程序时,我不断收到如下错误消息:

C4996: 'std::_Copy_impl': Function call with parameters that may be unsafe -     this call relies on the caller to check that the passed values are correct. To disable this warning, use -D_SCL_SECURE_NO_WARNINGS. See documentation on how to use Visual C++ 'Checked Iterators' c:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 2132    1   ConsoleApplication1

我已尝试过警告,但未找到与此错误相关的任何结果。有没有更好的方法来编写这段代码?我对C ++很陌生,对Boost实际工作方式并不太了解。 我正在使用Visual Studio 2013并提升1.60.0。

1 个答案:

答案 0 :(得分:0)

不是您问题的答案,而是解决方法。

我认为错误来自std::copy调用,您可以直接使用std::string中的data方法作为缓冲区数组来绕过它,而不是在{{1}中复制它}}

像这样:

boost::array