当我尝试用boost编译我的服务器时,我有很多错误.这是我的makefile:
NAME = serveur
SRCS = Serveur/main.cpp \
Serveur/Client.cpp \
Serveur/Commande.cpp \
Serveur/My_exception.cpp \
Serveur/Network.cpp \
Serveur/Server.cpp
#####################################################
OBJS = $(SRCS:.cpp=.o)
CC = g++
RM = rm -f
CFLAGS = -g -W -Wall -Werror
INCL = ./Serveur/boost_1_47_0
LIB = ./Serveur/boost_1_47_0/stage/lib/
NLIB = -lboost_system -lboost_system-mt -lboost_filesystem -lboost_filesystem-mt
#####################################################
$(NAME) : $(OBJS)
@$(CC) $(OBJS) -I$(INCL) -L$(LIB) $(NLIB) -o $(NAME)
@printf "\n \033[33m[Message]\033[39m Compilation under Linux done\n\n"
.cpp.o :
@$(CC) $(CFLAGS) -I$(INCL) -L$(LIB) $(NLIB) -c $< -o $@
@printf " \033[34m[Compilation]\033[39m %s\n" $< …Run Code Online (Sandbox Code Playgroud) 我想从配置文件中重新加载一些值。我知道,po::store如果值存在于中,则不会更改variables_map。是否有替代方法即使值已经存在也可以替换?
我尝试从中删除要重新加载的值variables_map,但是po::store无论如何都不会添加新值(即使也无法访问旧值)。
我以为我在下面的例子中找到了答案,但并不完全.
boost::asio::ip::udp::socket socket(io_service);
...
boost::asio::ip::address_v4 local_interface =
boost::asio::ip::address_v4::from_string("1.2.3.4");
boost::asio::ip::multicast::outbound_interface option(local_interface);
socket.set_option(option);
Run Code Online (Sandbox Code Playgroud)
如何映射eth0到适当的outbound_interface选项?
关闭异步boost asio tcp服务器的正确方法是什么?我目前的解决方案通常在析构函数中死锁.为什么?
class connection;
typedef std::set<shared_ptr<connection>> connection_set;
class connection : public enable_shared_from_this<connection>
{
shared_ptr<tcp::socket> socket_;
std::array<char, 8192> data_;
shared_ptr<connection_set> connection_set_;
public:
static shared_ptr<connection> create(shared_ptr<tcp::socket> socket, shared_ptr<connection_set> connection_set)
{
auto con = shared_ptr<connection>(new connection(std::move(socket), std::move(connection_set)));
con->read_some();
return con;
}
void on_next(const event& e)
{
// async_write_some ...
}
private:
connection(shared_ptr<tcp::socket> socket, shared_ptr<connection_set> connection_set)
: socket_(std::move(socket))
, connection_set_(std::move(connection_set))
{
}
void handle_read(const boost::system::error_code& error, size_t bytes_transferred)
{
if(!error)
{
on_read(std::string(data_.begin(), data_.begin() + bytes_transferred));
read_some();
}
else if (error != boost::asio::error::operation_aborted)
connection_set_->erase(shared_from_this()); …Run Code Online (Sandbox Code Playgroud) 我使用boost.asio来实现网络通信.在主线程中,我创建TCP套接字并连接远程机器.然后启动一个工作线程从套接字读取数据.在主线程中,相同的套接字用于发送数据.这意味着在没有互斥锁的两个线程中使用相同的套接字.代码粘贴在下面.关于套接字的读写功能有什么问题吗?
boost::asio::io_service m_io_service;
boost::asio::ip::tcp::socket m_socket(m_io_service);
boost::thread* m_pReceiveThread;
void Receive();
void Connect()
{
boost::asio::ip::tcp::endpoint endpoint(boost::asio::ip::address::from_string("127.0.0.1"), 13);
m_socket.connect(endpoint);
m_pReceiveThread = new boost::thread(Receive);
}
void Send()
{
std::wstring strData(L"Message");
boost::system::error_code error;
const std::size_t byteSize = boost::asio::write(m_socket, boost::asio::buffer(strData), error);
}
void Receive()
{
for (;;)
{
boost::array<wchar_t, 128> buf = {0};
boost::system::error_code error;
const std::size_t byteSize = m_socket.read_some(boost::asio::buffer(buf), error);
// Dispatch the received data through event notification.
}
}
int main()
{
Connect();
while(true)
{
boost::this_thread::sleep( boost::posix_time::seconds(1));
Send();
}
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我在Qt中编写TCP服务器,它将提供大文件.应用逻辑如下:
bytesWritten看起来像:
Streamer.h:
...
private:
QFile *m_file;
char m_readBuffer[64 * 1024];
QTcpSocket *m_socket;
...
Streamer.cpp
...
void Streamer::bytesWritten() {
if (m_socket->bytesToWrite() <= 0) {
const int bytesRead = m_file->read(m_readBuffer, 64 * 1024);
m_socket->write(m_readBuffer, bytesRead);
}
}
...
Run Code Online (Sandbox Code Playgroud)
所以基本上我只是在完全写入所有待处理数据时才写入新数据.我认为这是最不同步的方式.
一切都正常,但是当有很多同时发生的客户时它很慢.
有大约5个客户端 - 我从该服务器下载速度大约1 MB/s(最大的家庭互联网连接)
约有140个客户端 - 下载速度约为100-200 KB/s.
服务器的互联网连接速度为10 Gbps,140个客户端的使用速度约为100 Mbps,因此我不认为这是问题所在.
服务器的内存使用量为140个客户端 - 100 MB的2GB可用
服务器的CPU使用率 - 最大20%
我正在使用端口800.
当端口800上有140个客户端并且通过它的下载速度就像是100-200 KB/s时,我在端口801上运行单独的副本,并且以1 MB/s的速度下载没有问题.
我的猜测是,不知何故,Qt的事件调度(或套接字通知?)太慢,无法处理所有这些事件.
我试过了:
码:
main.cpp:
#include <sched.h>
int startWorker(void …Run Code Online (Sandbox Code Playgroud) 我本来打算在我的程序中的线程这将等待两个文件描述符,一个用于插座,第二个为FD描述文件系统(特别是等着看是否有新的文件被添加到一个目录).因为我希望很少看到任何新文件添加或在我即将到来的新的TCP消息想有一个线程等待任一输入和处理取其输入时,它occures而不是有独立的线程困扰检测.
然后我(终于!)获得了"老板"的许可,使用了boost.所以现在我想用boost:asio替换基本套接字.只是我遇到了一个小问题.似乎asio对它自己的select版本进行了修改,而不是提供一个我可以直接使用select的FD.这让我不确定我怎么能在这两个条件,新的文件和TCP输入块,在同一时间,当一个只选择工作和其他似乎并不支持使用选择.有一个简单的工作,我错过了吗?
我正在为学校制作一个程序,其中两个程序相互通信.到目前为止,我还没能连接这两个程序.每当我尝试连接到localhost:8888或127.0.0.1:8888时,都会出现错误"Host not found(authoritative)".
到目前为止我的代码是这样的:
Connection::Connection(std::string Arg) {
try
{
tcp::resolver resolver(io_service);
cout<<Arg<<endl;
tcp::resolver::query query(Arg, "daytime");
tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
tcp::resolver::iterator end;
tcp::socket socket(io_service);
socket_p = &socket;
boost::system::error_code error = boost::asio::error::host_not_found;
while (error && endpoint_iterator != end)
{
socket.close();
socket.connect(*endpoint_iterator++, error);
}
if (error)
throw boost::system::system_error(error);
}
catch (std::exception& e)
{
std::cerr << e.what() << std::endl;
}
}
void Connection::Receiver() {
try{
for (;;)
{
boost::array<char, 128> buf;
boost::system::error_code error;
size_t len = socket_p->read_some(boost::asio::buffer(buf), error);
if (error == boost::asio::error::eof)
break; …Run Code Online (Sandbox Code Playgroud) 我想验证我为Linux编写和编译的C++应用程序的内存稳定性.它是一个网络应用程序,以每秒10-20个连接的速率响应远程客户端连接.从长远来看,内存增加到50MB,尽管应用程序正在调用删除...
调查显示Linux没有立即释放内存.所以这是我的问题:
如何强制Linux释放我实际释放的内存?至少我想这样做一次以验证内存稳定性.否则,是否有可靠的内存指示器可以报告我的应用实际持有的内存?
我正在尝试编译没有boost库的asio. http://think-async.com/Asio/Documentation
所以我做了
./configure --prefix=/raspberry/asio/product
Run Code Online (Sandbox Code Playgroud)
我收到了错误
checking boost/noncopyable.hpp usability... no
checking boost/noncopyable.hpp presence... no
checking for boost/noncopyable.hpp... no
Can't find boost headers. Please check the location of the boost
distribution and rerun configure using the --with-boost=DIR option.
Run Code Online (Sandbox Code Playgroud)
我认为这个asio不需要提升.
所以,接下来我用boost来抓取asio库并抓住boost文件夹并放入asio(非boost)文件夹.
但它仍然给我同样的错误.
c++ ×9
boost ×8
boost-asio ×7
interface ×1
linux ×1
makefile ×1
memory ×1
memory-leaks ×1
multicast ×1
performance ×1
qt ×1
qtcpserver ×1