Network
Provides the ability to work with TCP, UDP, and TLS.
- Example
- {{{
import fs2.Stream
import fs2.io.net.{Datagram, Network}
def send[F[_] : Network](datagram: Datagram): F[Unit] =
Network[F] .openDatagramSocket().use { socket =>
socket.write(packet)
}
}}}
In this example, theF[_]parameter tosendrequires theNetworkconstraint instead
of requiring the much more powerfulAsyncconstraint.
TheNetworkinstance has a set of global resources used for managing sockets. Alternatively,
use thesocketGroupanddatagramSocketGroupoperations to manage the lifecycle of underlying
resources.
An instance ofNetworkis available for any effectFwhich has anAsync[F]instance. - Companion
- object
Value members
Methods
Provides an isolated
The resulting socket group is shutdown during resource finalization, resulting in
closure of any sockets that were created.
SocketGroup[F] with the specified thread pool configuration.The resulting socket group is shutdown during resource finalization, resulting in
closure of any sockets that were created.
Note:
control over the lifecycle of the socket group.
Network is a SocketGroup so only use this operation if you need explicitcontrol over the lifecycle of the socket group.
- Value Params
- threadCount
-
number of threads to allocate in the fixed thread pool backing the NIO channel group
- threadFactory
-
factory used to create fixed threads
Provides an isolated
The resulting socket group is shutdown during resource finalization, resulting in
closure of any sockets that were created.
DatagramSocketGroup[F] with the specified thread configuration.The resulting socket group is shutdown during resource finalization, resulting in
closure of any sockets that were created.
Note:
control over the lifecycle of the socket group.
Network is a DatagramSocketGroup so only use this operation if you need explicitcontrol over the lifecycle of the socket group.
- Value Params
- threadFactory
-
factory used to create selector thread
Inherited methods
def serverResource(address: Option[Host], port: Option[Port], options: List[SocketOption]): Resource[F, (SocketAddress[IpAddress], Stream[F, Socket[F]])]
Like server but provides the
SocketAddress of the bound server socket before providing accepted sockets.- Inhertied from
- SocketGroup
Opens a TCP connection to the specified server.
The connection is closed when the resource is released.
- Value Params
- options
-
socket options to apply to the underlying socket
- to
-
address of remote server
- Inhertied from
- SocketGroup
def openDatagramSocket(address: Option[Host], port: Option[Port], options: List[SocketOption], protocolFamily: Option[ProtocolFamily]): Resource[F, DatagramSocket[F]]
Creates a UDP socket bound to the specified address.
- Value Params
- address
-
address to bind to; defaults to all interfaces
- options
-
socket options to apply to the underlying socket
- port
-
port to bind to; defaults to an ephemeral port
- protocolFamily
-
protocol family to use when opening the supporting
DatagramChannel
- Inhertied from
- DatagramSocketGroup
def server(address: Option[Host], port: Option[Port], options: List[SocketOption]): Stream[F, Socket[F]]
Creates a TCP server bound to specified address/port and returns a stream of
client sockets -- one per client that connects to the bound address/port.
client sockets -- one per client that connects to the bound address/port.
When the stream terminates, all open connections will terminate as well.
- Value Params
- address
-
address to accept connections from; none for all interfaces
- options
-
socket options to apply to the underlying socket
- port
-
port to bind
- Inhertied from
- SocketGroup