AsyncSocketChannel

abstract class AsyncSocketChannel extends AutoCloseable

An asynchronous channel for reading, writing, and manipulating a TCP socket.

On the JVM this is a wrapper around java.nio.channels.AsynchronousSocketChannel (class available since Java 7 for doing async I/O on sockets).

Example
 val asyncSocketChannel = AsyncSocketChannel()
 val connectF = asyncSocketChannel.connect(new InetSocketAddress("google.com", 80))
 val bytes = ByteBuffer.wrap("Hello world!".getBytes("UTF-8"))
 val writeF = connectF.flatMap(_ => asyncSocketChannel.write(bytes, None))
 writeF.onComplete {
   case Success(nr) =>
     println(f"Bytes written: $nr%d")
  case Failure(exc) =>
     println(s"ERR: $exc")
 }
Companion
object
trait AutoCloseable
class Object
trait Matchable
class Any

Value members

Abstract methods

def connect(remote: InetSocketAddress, cb: Callback[Throwable, Unit]): Unit

Connects this channel.

Connects this channel.

Value Params
cb

is the callback to be called with the result, once this asynchronous operation is complete

remote

the remote address to which this channel is to be connected

def localAddress(): Option[InetSocketAddress]

Asks the socket address that this channel's socket is bound to

Asks the socket address that this channel's socket is bound to

def read(dst: ByteBuffer, cb: Callback[Throwable, Int], timeout: Option[Duration]): Unit

Reads a sequence of bytes from this channel into the given buffer

Reads a sequence of bytes from this channel into the given buffer

Value Params
cb

is the callback to be called with the result, once this asynchronous operation is complete . For this method it signals the number of bytes read or -1 if no bytes could be read because the channel has reached end-of-stream

dst

is the buffer holding the bytes read on completion

timeout

an optional maximum time for the I/O operation to complete

def remoteAddress(): Option[InetSocketAddress]

Asks the remote address to which this channel's socket is connected

Asks the remote address to which this channel's socket is connected

def stopReading(): Unit

Indicates that this channel will not read more data - end-of-stream indication

Indicates that this channel will not read more data - end-of-stream indication

def stopWriting(): Unit

Indicates that this channel will not write more data - end-of-stream indication

Indicates that this channel will not write more data - end-of-stream indication

def write(src: ByteBuffer, cb: Callback[Throwable, Int], timeout: Option[Duration]): Unit

Writes a sequence of bytes to this channel from the given buffer

Writes a sequence of bytes to this channel from the given buffer

Value Params
cb

is the callback to be called with the result, once this asynchronous operation is complete . For this method it signals the number of bytes that were written

src

is the buffer holding the sequence of bytes to write

timeout

an optional maximum time for the I/O operation to complete

Concrete methods

def connect(remote: InetSocketAddress): Future[Unit]

Connects this channel.

Connects this channel.

Value Params
remote

the remote address to which this channel is to be connected

def read(dst: ByteBuffer, timeout: Option[Duration]): Future[Int]

Reads a sequence of bytes from this channel into the given buffer

Reads a sequence of bytes from this channel into the given buffer

Value Params
dst

is the buffer holding the bytes read on completion

timeout

an optional maximum time for the I/O operation to complete

Returns

the number of bytes read or -1 if no bytes could be read because the channel has reached end-of-stream

def write(src: ByteBuffer, timeout: Option[Duration]): Future[Int]

Writes a sequence of bytes to this channel from the given buffer

Writes a sequence of bytes to this channel from the given buffer

Value Params
src

is the buffer holding the sequence of bytes to write

timeout

an optional maximum time for the I/O operation to complete

Returns

the number of bytes that were written

Inherited methods

@throws(java.lang.Exception)
def close(): Unit
Inherited from
AutoCloseable