0

I have some experience in network programming in C but in my new project I have/wnat to use c++. My question is now a bit general, are there any c++ specific libraries already available for the network programming or do I just use the same methods as in C? E.g. for getting IP address in C I would do something like:

  fd = socket(AF_INET, SOCK_DGRAM, 0);
  ifr.ifr_addr.sa_family = AF_INET;
  strncpy(ifr.ifr_name, "eth0", IFNAMSIZ-1);
  ioctl(fd, SIOCGIFADDR, &ifr);
  strncpy(ip, inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr), IP_ADDR_LEN);
  ioctl(fd, SIOCGIFNETMASK, &ifr);
  strncpy(netmask, inet_ntoa(((struct sockaddr_in *)&ifr.ifr_netmask)->sin_addr), IP_ADDR_LEN);

  close(fd);

I know I can simply use the same code in c++, but would there be any Classes with methods to make it "easier"?

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
wasp256
  • 5,943
  • 12
  • 72
  • 119
  • 1
    possible duplicate of [Best C/C++ Network Library](http://stackoverflow.com/questions/118945/best-c-c-network-library) – doron Dec 10 '14 at 16:03

1 Answers1

3

The library you want is asio. It's available standalone or as part of the boost libraries.

It's free and cross-platform, supports multi-threading, asynchronous sockets, synchronous sockets, timers, work queues....

It's about as close to being 'standard' as you can get without being in the c++ standard.

http://www.boost.org/doc/libs/1_57_0/doc/html/boost_asio.html

http://think-async.com/Asio

Richard Hodges
  • 68,278
  • 7
  • 90
  • 142
  • It looks as if boost asio [might be blessed](http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2014/n4243.html) by the C++ standards committee. They seem to have decided a clean room was impractical. It obviously isn't available now but it means an investment in boost asio code largely won't be wasted. – emsr Dec 10 '14 at 17:25
  • I'd say that is a wise choice by the standards committee - asio is flawless. – Richard Hodges Dec 10 '14 at 17:26