\section{Network Transparency}

All actor operations as well as sending messages are network transparent.
Remote actors are represented by actor proxies that forward all messages.
All functions shown in this section can be accessed by including the header \lstinline^"caf/io/all.hpp"^ and live in the namespace \lstinline^caf::io^.

\subsection{Publishing of Actors}

\begin{lstlisting}
uint16_t publish(actor whom, uint16_t port,
                 const char* addr = nullptr,
                 bool reuse_addr = false)
\end{lstlisting}

The function \lstinline^publish^ binds an actor to a given port.
To choose the next high-level port available for binding, one can specify \lstinline^port == 0^ and retrieves the bound port as return value.
The return value is equal to \lstinline^port^ if \lstinline^port != 0^.
The function throws \lstinline^network_error^ if socket related errors occur or \lstinline^bind_failure^ if the specified port is already in use.
The optional \lstinline^addr^ parameter can be used to listen only to the given address.
Otherwise, the actor accepts all incoming connections (\lstinline^INADDR_ANY^).
The flag \lstinline^reuse_addr^ controls the behavior when binding an IP
address to a port, with the same semantics as the BSD socket flag \lstinline^SO_REUSEADDR^.
For example, if \lstinline^reuse_addr = false^, binding two sockets to 0.0.0.0:42 and 10.0.0.1:42 will fail with \texttt{EADDRINUSE} since 0.0.0.0 includes 10.0.0.1. 
With \lstinline^reuse_addr = true^ binding would succeed because 10.0.0.1 and
0.0.0.0 are not literally equal addresses.


\begin{lstlisting}
publish(self, 4242);
self->become (
  [](ping_atom, int i) {
    return std::make_tuple(pong_atom::value, i);
  }
);
\end{lstlisting}

To close a socket, e.g., to allow other actors to be published at the port, the function \lstinline^unpublish^ can be used.
This function is called implicitly if a published actor terminates.

\begin{lstlisting}
void unpublish(caf::actor whom, uint16_t port)
\end{lstlisting}

\clearpage
\subsection{Connecting to Remote Actors}

\begin{lstlisting}
actor remote_actor(const char* host, std::uint16_t port)
\end{lstlisting}

The function \lstinline^remote_actor^ connects to the actor at given host and port.
A \lstinline^network_error^ is thrown if the connection failed.

\begin{lstlisting}
auto pong = remote_actor("localhost", 4242);
self->send(pong, ping_atom::value, 0);
self->become (
  [=](pong_value, int i) {
    if (i >= 10) {
      self->quit();
      return;
    }
    self->send(pong, ping_atom::value, i + 1);
  }
);
\end{lstlisting}
