如何使用自定义命令对boost库套接字进行io_control

时间:2017-04-04 16:01:12

标签: boost-asio ioctl

我试图使用boost库为c ++风格的“ioctl”创建一个相同的函数。

这是我的“c”样式代码:

int sockfd;
char * id;
struct iwreq wreq;

memset(&wreq, 0, sizeof(struct iwreq));
sprintf(wreq.ifr_name, IW_INTERFACE);

if((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
    fprintf(stderr, "Cannot open socket \n");
    fprintf(stderr, "errno = %d \n", errno);
    fprintf(stderr, "Error description is : %s\n",strerror(errno));
    exit(1);
}
printf("Socket opened successfully \n");


id = malloc(IW_ESSID_MAX_SIZE+1);
wreq.u.essid.pointer = id;
if (ioctl(sockfd, SIOCGIWESSID, &wreq)) {
    fprintf(stderr, "Get ESSID ioctl failed \n");
    fprintf(stderr, "errno = %d \n", errno);
    fprintf(stderr, "Error description : %s\n",strerror(errno));
    exit(2);
}
printf("IOCTL Successfull\n");
printf("ESSID is %s\n", wreq.u.essid.pointer);

我找到了一些相关的例子,但我不清楚如何正确使用它。 example

主要功能:

boost::asio::ip::udp::socket socket(io_service);
struct iwreq wreq
memset(&wreq, 0, sizeof(struct iwreq));
sprintf(wreq.ifr_name, IW_INTERFACE);
id = malloc(IW_ESSID_MAX_SIZE+1);
wreq.u.essid.pointer = id;

boost::asio::detail::io_control::myCommand command;
command.set(&wreq);
boost::system::error_code ec;
socket.io_control(command, ec);
if (ec)
{
  // An error occurred.
}

自定义命令:

#include <boost/asio/detail/config.hpp>
#include <cstddef>
#include <boost/config.hpp>
#include <boost/asio/detail/socket_types.hpp>

#include <boost/asio/detail/push_options.hpp>

namespace boost {
namespace asio {
namespace detail {
namespace io_control {

// I/O control command for getting number of bytes available.
class myCommand
{
public:
  // Default constructor.
  myCommand()
    : value_(0)
  {
  }


  // Get the name of the IO control command.
  int name() const
  {
    return static_cast<int>(SIOCGIWESSID);
  }

  // Set the value of the I/O control command.
  void set(struct iwreq* value)
  {
    value_ = static_cast<detail::ioctl_arg_type>(value);
  }

  // Get the current value of the I/O control command.
  std::size_t get() const
  {
    return static_cast<struct iwreq*>(value_);
  }

  // Get the address of the command data.
  detail::ioctl_arg_type* data()
  {
    return &value_;
  }

  // Get the address of the command data.
  const detail::ioctl_arg_type* data() const
  {
    return &value_;
  }

private:
  detail::ioctl_arg_type value_;
};

} // namespace io_control
} // namespace detail
} // namespace asio
} // namespace boost

但是,代码不起作用。 如果您有任何示例代码或解决方案,请告诉我。 谢谢。

0 个答案:

没有答案