masterconqueror masterconqueror - 27 days ago 17
C++ Question

Sending data from a boost asio client

I am trying to write a program which can send data over a network to my Java server listening on a port.

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


When I use this program I keep getting error message as below:

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


I have tried the warning and not found any result related to this error. Is there a better way to write this code? I am really new to C++ and do not have much idea about how Boost is actually working.
I am using Visual Studio 2013 and boost 1.60.0.

Answer

Not really the answer to your question but rather a workaround.

I think the error comes from the std::copy call, you could bypass it by using the data method from std::string directly as the buffer array instead of copying it in your boost::array.

Like this :

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