我正在使用boost Asio进行一些测试,并且我不明白为什么不会调用async_write函数的处理程序(女巫是lambda函数)。
#include <iostream>
#include <boost/asio.hpp>
using namespace boost::asio;
int main() {
io_service service;
std::string str = std::string("HTTP/1.1 200 OK\r\n ......");
ip::tcp::acceptor my_acceptor{ service, ip::tcp::endpoint(ip::tcp::v4(), 6767) };
ip::tcp::socket my_socket{ service };
my_acceptor.async_accept(my_socket, [&my_socket, str](boost::system::error_code) {
std::cout << "connected " << std::endl;
async_write(my_socket, boost::asio::buffer(str), [&my_socket](boost::system::error_code, std::size_t) {
std::cout << "Data written" << std::endl;
});
});
service.run();
}
&#34;写入数据&#34;永远不会出现,并且消息不会发送到客户端 这段代码有什么问题?
答案 0 :(得分:0)
代码运行正常。
您是否正在以足够的权限运行以侦听端口80?
你确定没有其他人在那里听吗?
尝试
//Input events, hot observable
var source = Observable.Interval(TimeSpan.FromSeconds(1))
.Select(i => i.ToString())
.Publish().RefCount();
//Simulate pausing from Keyboard, not actually relevant within this answer
var pauseObservable = Observable.FromEventPattern<KeyPressEventHandler, KeyPressEventArgs>(
k => KeyPressed += k, k => KeyPressed -= k)
.Select(i => i.EventArgs.PressedKey)
.Select(i => i == ConsoleKey.Spacebar) //space is pause, others are unpause
.DistinctUntilChanged();
//Let events through when not paused
var notPausedEvents = source.Zip(pauseObservable.MostRecent(false), (value, paused) => new {value, paused})
.Where(i => !i.paused) //not paused
.Select(i => i.value)
.Subscribe(Console.WriteLine);
//When paused, buffer until not paused
var pausedEvents = pauseObservable.Where(i => i)
.Subscribe(_ =>
source.BufferUntil(pauseObservable.Where(i => !i))
.Select(i => String.Join(Environment.NewLine, i))
.Subscribe(Console.WriteLine));
E.g。
cursor.close()