简体   繁体   English

无法打开UDP套接字

[英]Unable to open UDP socket

I'm trying to create a UDP socket with boost to send and receive data. 我正在尝试创建带有Boost的UDP套接字来发送和接收数据。 Here's the relevant code: 以下是相关代码:

// Create IO service
mIoService = std::make_unique<boost::asio::io_service>();

// Create local endpoint on random port
mLocalEndpoint = std::make_unique<udp::endpoint>(udp::v4(), 0);

// Create socket
mSocket = std::make_unique<udp::socket>(*mIoService, *mLocalEndpoint);
mSocket->open(mLocalEndpoint->protocol());

This gives me the following exception: 这给了我以下例外:

open: Already open 打开:已经打开

I have fiddled around with this code for quite some time now, but the result stays the same. 我已经在这段代码上摆弄了很长时间,但是结果保持不变。 What am I missing here? 我在这里想念什么?

It looks like you try to create a UDP end point on port 0 . 似乎您尝试在端口0上创建UDP端点。 I'd assume that it may be already used, so you can try to change a port: 我假设它可能已经被使用过,所以您可以尝试更改端口:

mLocalEndpoint = std::make_unique<udp::endpoint>(udp::v4(), 40000);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM