| Home | Libraries | People | FAQ | More

#### The BSD Socket API and Boost.Asio

The Boost.Asio library includes a low-level socket interface based on the BSD socket API, which is widely implemented and supported by extensive literature. It is also used as the basis for networking APIs in other languages, like Java. This low-level interface is designed to support the development of efficient and scalable applications. For example, it permits programmers to exert finer control over the number of system calls, avoid redundant data copying, minimise the use of resources like threads, and so on.

Unsafe and error prone aspects of the BSD socket API not included. For example, the use of `int` to represent all sockets lacks type safety. The socket representation in Boost.Asio uses a distinct type for each protocol, e.g. for TCP one would use `ip::tcp::socket`, and for UDP one uses `ip::udp::socket`.

The following table shows the mapping between the BSD socket API and Boost.Asio:

BSD Socket API Elements | Equivalents in Boost.Asio
--- | ---
socket descriptor - `int` (POSIX) or `SOCKET` (Windows) | For TCP: ip::tcp::socket, ip::tcp::acceptor For UDP: ip::udp::socket basic_socket, basic_stream_socket, basic_datagram_socket, basic_raw_socket
`in_addr`, `in6_addr` | ip::address, ip::address_v4, ip::address_v6
`sockaddr_in`, `sockaddr_in6` | For TCP: ip::tcp::endpoint For UDP: ip::udp::endpoint ip::basic_endpoint
`accept()` | For TCP: ip::tcp::acceptor::accept() basic_socket_acceptor::accept()
`bind()` | For TCP: ip::tcp::acceptor::bind(), ip::tcp::socket::bind() For UDP: ip::udp::socket::bind() basic_socket::bind()
`close()` | For TCP: ip::tcp::acceptor::close(), ip::tcp::socket::close() For UDP: ip::udp::socket::close() basic_socket::close()
`connect()` | For TCP: ip::tcp::socket::connect() For UDP: ip::udp::socket::connect() basic_socket::connect()
`getaddrinfo()`, `gethostbyaddr()`, `gethostbyname()`, `getnameinfo()`, `getservbyname()`, `getservbyport()` | For TCP: ip::tcp::resolver::resolve(), ip::tcp::resolver::async_resolve() For UDP: ip::udp::resolver::resolve(), ip::udp::resolver::async_resolve() ip::basic_resolver::resolve(), ip::basic_resolver::async_resolve()
`gethostname()` | ip::host_name()
`getpeername()` | For TCP: ip::tcp::socket::remote_endpoint() For UDP: ip::udp::socket::remote_endpoint() basic_socket::remote_endpoint()
`getsockname()` | For TCP: ip::tcp::acceptor::local_endpoint(), ip::tcp::socket::local_endpoint() For UDP: ip::udp::socket::local_endpoint() basic_socket::local_endpoint()
`getsockopt()` | For TCP: ip::tcp::acceptor::get_option(), ip::tcp::socket::get_option() For UDP: ip::udp::socket::get_option() basic_socket::get_option()
`inet_addr()`, `inet_aton()`, `inet_pton()` | ip::address::from_string(), ip::address_v4::from_string(), ip_address_v6::from_string()
`inet_ntoa()`, `inet_ntop()` | ip::address::to_string(), ip::address_v4::to_string(), ip_address_v6::to_string()
`ioctl()` | For TCP: ip::tcp::socket::io_control() For UDP: ip::udp::socket::io_control() basic_socket::io_control()
`listen()` | For TCP: ip::tcp::acceptor::listen() basic_socket_acceptor::listen()
`poll()`, `select()`, `pselect()` | io_service::run(), io_service::run_one(), io_service::poll(), io_service::poll_one() Note: in conjunction with asynchronous operations.
`readv()`, `recv()`, `read()` | For TCP: ip::tcp::socket::read_some(), ip::tcp::socket::async_read_some(), ip::tcp::socket::receive(), ip::tcp::socket::async_receive() For UDP: ip::udp::socket::receive(), ip::udp::socket::async_receive() basic_stream_socket::read_some(), basic_stream_socket::async_read_some(), basic_stream_socket::receive(), basic_stream_socket::async_receive(), basic_datagram_socket::receive(), basic_datagram_socket::async_receive()
`recvfrom()` | For UDP: ip::udp::socket::receive_from(), ip::udp::socket::async_receive_from() basic_datagram_socket::receive_from(), basic_datagram_socket::async_receive_from()
`send()`, `write()`, `writev()` | For TCP: ip::tcp::socket::write_some(), ip::tcp::socket::async_write_some(), ip::tcp::socket::send(), ip::tcp::socket::async_send() For UDP: ip::udp::socket::send(), ip::udp::socket::async_send() basic_stream_socket::write_some(), basic_stream_socket::async_write_some(), basic_stream_socket::send(), basic_stream_socket::async_send(), basic_datagram_socket::send(), basic_datagram_socket::async_send()
`sendto()` | For UDP: ip::udp::socket::send_to(), ip::udp::socket::async_send_to() basic_datagram_socket::send_to(), basic_datagram_socket::async_send_to()
`setsockopt()` | For TCP: ip::tcp::acceptor::set_option(), ip::tcp::socket::set_option() For UDP: ip::udp::socket::set_option() basic_socket::set_option()
`shutdown()` | For TCP: ip::tcp::socket::shutdown() For UDP: ip::udp::socket::shutdown() basic_socket::shutdown()
`sockatmark()` | For TCP: ip::tcp::socket::at_mark() basic_socket::at_mark()
`socket()` | For TCP: ip::tcp::acceptor::open(), ip::tcp::socket::open() For UDP: ip::udp::socket::open() basic_socket::open()
`socketpair()` | local::connect_pair() Note: POSIX operating systems only.