- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我尝试使用 async_receive_from 方法来获取数据。但是当我开始时,程序不会等待超时或正在读取某些内容。我知道数据正在端口(wireshark)上传入。这可能是什么原因。我尝试使用来自 boost website 的不同示例.当我调试代码时,我总是得到错误代码 125 (operation_aborted)。这是我的代码的相关部分:
UDPCommunication::UDPCommunication(){
m_portReceive = 2041;
m_byteArrayLen = 0;
m_lengthReceive = 0;
m_dataReceived = false;
}
void UDPCommunication::openSocketReceive(){
try{
m_socketReceive = shared_ptr<udp::socket>(
new udp::socket(m_ioService,
udp::endpoint(udp::v4(), m_portReceive)));
m_receiveTimeout = shared_ptr<boost::asio::deadline_timer>(
new boost::asio::deadline_timer(m_ioService));
m_receiveTimeout->async_wait(
boost::bind(&UDPCommunication::udpReceiveTimeoutHandler, this,
boost::asio::placeholders::error, 0));
m_dataReceived = false;
} catch(std::exception& e){
std::cerr << e.what() << std::endl;
ErrorLogging::log(e);
}
}
void UDPCommunication::udpReceiveWithTimeout(long time){
try{
boost::system::error_code ec;
m_receiveTimeout->expires_from_now(boost::posix_time::seconds(time));
ec = boost::asio::error::would_block;
m_lengthReceive = 0;
m_socketReceive->async_receive_from(buffer(m_bufferReceive),
m_endpointReceive,
boost::bind(&UDPCommunication::udpReceiveTimeoutHandler, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
do{
m_ioService.run_one();
}while(ec == boost::asio::error::would_block);
} catch(std::exception& e){
std::cerr << e.what() << std::endl;
ErrorLogging::log(e);
}
}
bool UDPCommunication::udpReceiveTimeoutHandler(
const boost::system::error_code& ec, size_t size){
try{
m_socketReceive->cancel();
m_receiveTimeout->cancel();
if(ec != boost::asio::error::would_block){
m_dataReceived = false;
return false;
}else{
m_lengthReceive = sizeof(m_bufferReceive);
m_vectorBuffer.resize(m_lengthReceive);
int i = 0;
for(std::vector<unsigned char>::iterator it =
m_vectorBuffer.begin(); it != m_vectorBuffer.end(); ++it){
*it = m_bufferReceive[i];
++i;
}
m_dataReceived = true;
return true;
}
} catch(std::exception& e){
std::cerr << e.what() << std::endl;
ErrorLogging::log(e);
return false;
}
return false;
}
bool UDPCommunication::dataReceived(){
return m_dataReceived;
}
void main(){
udpCommunication = shared_ptr<UDPCommunication>(new UDPCommunication());
udpCommunication->openSocketReceive();
udpCommunication->udpReceiveWithTimeout(5);
bool dataReceived = udpCommunication->dataReceived();
std::cout<< dataReceived << std::endl; //is data coming?
}
上层问题的解。此代码工作正常:
int m_portReceive;
int m_byteArrayLen;
io_service m_ioService;
udp::endpoint m_endpointReceive;
shared_ptr<udp::socket> m_socketReceive;
shared_ptr<boost::asio::deadline_timer> m_receiveTimeout;
std::vector<unsigned char> m_vectorBuffer;
unsigned char m_bufferReceive[128];
size_t m_lengthReceive;
bool m_dataReceived;
bool m_timeoutFired;
boost::mutex m_mutex;
UDPCommunication::UDPCommunication(){
m_portReceive = 2041;
m_byteArrayLen = 0;
m_lengthReceive = 0;
m_dataReceived = false;
m_timeoutFired = false;
}
void UDPCommunication::openSocketReceive(){
try{
m_socketReceive = shared_ptr<udp::socket>(
new udp::socket(m_ioService,
udp::endpoint(udp::v4(), m_portReceive)));
m_receiveTimeout = shared_ptr<boost::asio::deadline_timer>(
new boost::asio::deadline_timer(m_ioService));
m_receiveTimeout->expires_at(boost::posix_time::pos_infin);
UDPCommunication::udpTimeoutHandler();
m_receiveTimeout->async_wait(
boost::bind(&UDPCommunication::udpTimeoutHandler, this));
m_dataReceived = false;
} catch(std::exception& e){
std::cerr << e.what() << std::endl;
ErrorLogging::log(e);
}
}
void UDPCommunication::udpReceiveWithTimeout(long time){
try{
boost::system::error_code ec;
m_receiveTimeout->expires_from_now(
boost::posix_time::time_duration(
boost::posix_time::seconds(time)));
m_socketReceive->async_receive_from(buffer(m_bufferReceive),
m_endpointReceive,
boost::bind(&UDPCommunication::udpReceiveHandler, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
do{
m_ioService.run_one();
}while((m_lengthReceive == 0)
&& (m_timeoutFired == false));
} catch(std::exception& e){
std::cerr << e.what() << std::endl;
ErrorLogging::log(e);
}
}
void UDPCommunication::udpReceiveHandler(const boost::system::error_code& ec,
size_t size){
try{
m_mutex.lock();
if(m_timeoutFired == false){
if(size > 0){
m_socketReceive->cancel();
m_receiveTimeout->cancel();
m_lengthReceive = size;
m_vectorBuffer.resize(m_lengthReceive);
int i = 0;
for(std::vector<unsigned char>::iterator it =
m_vectorBuffer.begin(); it != m_vectorBuffer.end();
++it){
*it = m_bufferReceive[i];
++i;
}
m_dataReceived = true;
}else{
m_lengthReceive = 0;
m_socketReceive->async_receive_from(buffer(m_bufferReceive),
m_endpointReceive,
boost::bind(&UDPCommunication::udpReceiveHandler, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
}
m_mutex.unlock();
} catch(std::exception& e){
std::cerr << e.what() << std::endl;
ErrorLogging::log(e);
m_mutex.unlock();
}
}
void UDPCommunication::udpTimeoutHandler(){
try{
m_mutex.lock();
if(m_lengthReceive == 0){
if(m_receiveTimeout->expires_at()
<= deadline_timer::traits_type::now()){
std::cout << "timeout: no data received from modem"
<< std::endl;
m_socketReceive->cancel();
m_dataReceived = false;
m_receiveTimeout->expires_at(boost::posix_time::pos_infin);
m_timeoutFired = true;
}
m_receiveTimeout->async_wait(
boost::bind(&UDPCommunication::udpTimeoutHandler, this));
}
m_mutex.unlock();
} catch(std::exception& e){
std::cerr << e.what() << std::endl;
ErrorLogging::log(e);
m_mutex.unlock();
}
}
bool UDPCommunication::dataReceived(){
return m_dataReceived;
}
void main(){
udpCommunication = shared_ptr<UDPCommunication>(new UDPCommunication());
udpCommunication->openSocketReceive();
udpCommunication->udpReceiveWithTimeout(5);
bool dataReceived = udpCommunication->dataReceived();
std::cout<< dataReceived << std::endl; //is data coming?
//now do something with data...
}
最佳答案
请注意,我没有检查每一行是否有其他错误,但我想到了两个问题:
您在 deadline_timer 上进行 async_wait,而没有先设置过期时间。
您使用 io_service.run_one() 而无需调用 io_service.reset()。
我不确定第一个是做什么的,但第二个是什么意思,一旦 run_one() 第一次返回(通过发布任何处理程序或停止蜂鸣),它会在每次再次调用时立即返回,没有做好它的工作。
可能第一个调用您的超时处理程序带有 operation_aborted,第二个点阻止您的 io_service 做任何其他事情。
编辑:也有问题
do{
m_ioService.run_one();
}while(ec == boost::asio::error::would_block);
如果你想让 run_one() 方法改变 ec,你必须像这样传递它:run_one(ec)实际上,ec 是该方法中的局部变量,不应被任何内容修改,因此始终保持 would_block。当然,在那种情况下,run_one 将不再抛出任何东西,而是将其结果存储在 ec 中。
关于c++ - Boost UDP async_receive_from 不等待超时或接收,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20236953/
我刚刚了解了 udp 校验和计算。 但我很困惑算法是否检测到所有错误。 最佳答案 当然不是。没有校验和可以检测到所有错误。 关于udp - UDP 校验和是否检测到所有错误?,我们在Stack Ove
下面这个函数有什么问题?? 它应该抛出错误,因为我没有在本地主机上运行 UDP 服务器。 int openUdpSocket(int port) { int sock,sin_size;
我正在尝试修改这两个程序。我想让 udpclient.c 能够接收消息并让 udpserver.c 将消息回显给客户端。我还想在 udpclient 发送消息之前先获取它们的时间标记消息。收到的消息应
我有相当简单的 UDP 服务器写在 c 上。 有时我需要知道在套接字中排队的所有 udp 数据包(字节)的当前长度。 据我了解,getsockopt 没有得到这样的信息。 欢迎使用 Linux 和 F
除了直播音乐/视频外,谁能告诉在哪里使用 UDP 协议(protocol)? UDP 的默认用例是什么? 最佳答案 其他任何您需要性能但如果数据包在途中丢失时可以生存的东西。例如,多人游戏浮现在脑海中
与 TCP 的监视方法相反,UDP 是否会在所有数据包可用时立即发送它们? 谢谢。 最佳答案 TCP 有拥塞控制,UDP 没有,因为它是无连接的。 但是您的问题涉及多个问题:发送消息是否会导致立即发送
我知道 UDP 本质上是不可靠的,但是当连接到 localhost 时,我希望内核以不同的方式处理连接,因为一切都可以在内部处理。那么在这种特殊情况下,UDP 是否被认为是一种可靠的协议(protoc
我正在尝试使用 flash 和 rtmfp 协议(protocol)开发一个实时视频聊天应用程序,但我有疑问rtmfp 如何保证连接对等点,尤其是当对等点位于不同网络时。 最佳答案 RTMFP 依靠中
我发现所有使用 Netty 4.0 的 TCP 服务器实现都使用了 ServerBootstrap 实例。 The biggest and only difference between a serv
对于个人 MMO 游戏项目,我正在 java 中实现一个自制的可靠的基于 UDP 的协议(protocol)。鉴于我当前的设置,我相信窥探者劫持 session 相对简单,因此为了防止这种情况,我借此
我正在尝试手动计算各种 UDP 数据包的校验和,但与 Wireshark 中显示的结果相比,我总是得到错误的结果。下面是我如何做到这一点的示例: Source Address: 192.168.0.1
我正在尝试手动计算各种 UDP 数据包的校验和,但与 Wireshark 中显示的结果相比,我总是得到错误的结果。下面是我如何做到这一点的示例: Source Address: 192.168.0.1
我有一个奇怪的问题。我有一个成功运行的 C++ (boost asio) P2P 应用程序,它可以在大多数 NAT 上运行。问题是,当我将初始启动端口号指定为 1000 时,它会检查 1000 是否空
带有数据源的短 radio 链路,需要通过 IPv6 的 1280 Kbps 吞吐量,使用 UDP 停止和等待协议(protocol),该区域内没有其他客户端或明显的噪声源。我到底如何才能计算出最佳数
似乎可以在没有有效负载的情况下发送 UDP 数据包。 我能想到的唯一不需要有效载荷的就是用于 NAT 打洞。 这还能用来做什么? 这与我之前的问题有关Under Linux, can recv eve
我有一个客户端,我无法更改其代码 - 但我想(重新)使用 编写代码ZeroMQ socket 。 客户端同时使用原始 TCP 和原始 UDP socket 。 我知道我可以使用 ZMQ_ROUTER_
网络 4.0.24 我通过 UDP 传递 XML。收到 UPD 数据包时,数据包的长度始终为 2048,截断消息。尽管如此,我尝试将接收缓冲区大小设置为更大的值(4096、8192、65536),但它
我正在尝试编写一个有关 UDP 连接的简单程序来了解它们。我已经实现了一些基本的事情,但是当我尝试发送并取回我发送的内容时,我遇到了一些问题,例如, 当我这样做时;发送一个字符串 “asd”到服务器我
当我检查时,我在 UDP 客户端每 100 毫秒从服务器发送 UDP 数据包 接收频率不等于 100 毫秒,有时它要少得多,例如 3 毫秒…10 毫秒。 我知道UDP client server是异步
海友我是一个学习winsock2的新手。以下是我的udp服务器和客户端程序。 我这个程序客户端不知道服务器的IP地址,只知道端口。但是服务器会在整个网络中广播一条消息。 当客户端收到消息时,它会回溯服
我是一名优秀的程序员,十分优秀!