# IO:SocketChannel

### Import List

        IO
        IO:Address
        IO:Socket

Class List
Channel |
ServerChannel |

Class Summary: Channel [Detail]

      +---IO.Channel
           |
           +---IO.ByteChannel
                |
                +--IO:SocketChannel.Channel

Field Summary
connected-: BOOLEAN
connectionPending-: BOOLEAN
socket-: Socket
Inherited Fields
From IO.Channel: readyOps
Constructor Summary
Connect(Socket): Channel
Open(): Channel
Method Summary
Close() Cancel all selection keys registered for the channel ch and close the channel.
Connect(Socket): BOOLEAN
FileDescriptor(): FileDescriptor Return the file descriptor of the channel.
FinishConnect(): BOOLEAN
Read(VAR ARRAY OF BYTE, LONGINT, LONGINT): LONGINT Read a sequence of bytes from the channel into the buffer.
SetBlocking(BOOLEAN) Change the channel's blocking mode.
Write(ARRAY OF BYTE, LONGINT, LONGINT): LONGINT Write a sequence of bytes to this channel from the buffer.
Inherited Methods
From IO.Channel: Close, CloseAndRegister, FileDescriptor, KeyForSelector, RegisterWithSelector, SetBlocking, SetPos From IO.ByteChannel: Flush, INIT, Read, TransferTo, Write

Class Summary: ServerChannel [Detail]

      +---IO.Channel
           |
           +--IO:SocketChannel.ServerChannel

Field Summary
socket-: Server
Inherited Fields
From IO.Channel: readyOps
Constructor Summary
OpenServer(): ServerChannel
Method Summary
Accept(): Channel Accepts a connection request from a remote socket.
FileDescriptor(): FileDescriptor Return the file descriptor of the channel.
SetBlocking(BOOLEAN) Change the channel's blocking mode.
Inherited Methods
From IO.Channel: Close, CloseAndRegister, FileDescriptor, INIT, KeyForSelector, RegisterWithSelector, SetBlocking, SetPos

Class Detail: Channel

Field Detail

### connected

    FIELD connected-: BOOLEAN

### connectionPending

    FIELD connectionPending-: BOOLEAN

### socket

    FIELD socket-: Socket

Constructor Detail

### Connect

    PROCEDURE Connect(remote: Socket): Channel
      RAISES Error;

### Open

    PROCEDURE Open(): Channel
      RAISES Error;

Method Detail

### Close

    PROCEDURE (ch: Channel) Close()
      RAISES Error;

> Cancel all selection keys registered for the channel ch and close the channel.

> [Description inherited from Close]

> Redefines: Close, Close

### Connect

    PROCEDURE (ch: Channel) Connect(endPoint: Socket): BOOLEAN
      RAISES Error;

### FileDescriptor

    PROCEDURE (ch: Channel) FileDescriptor(): FileDescriptor

> Return the file descriptor of the channel. This is the integer number that is used to identify the channel on the level of the C library. If the channel is not associated with a file descriptor, result is ``-1`'.

> [Description inherited from FileDescriptor]

> Redefines: FileDescriptor, FileDescriptor

### FinishConnect

    PROCEDURE (ch: Channel) FinishConnect(): BOOLEAN
      RAISES Error;

### Read

    PROCEDURE (ch: Channel) Read(VAR buffer: ARRAY OF BYTE;
                   start: LONGINT;
                   length: LONGINT): LONGINT
      RAISES Error;

> Read a sequence of bytes from the channel into the buffer.

> An attempt is made to read up to length bytes from the channel. The bytes are written to buffer, starting at position start. Result is the number of bytes actually read, or ``-1`' if the read position is at the end of the channel, and length is not zero.

> A read operation might not read length bytes, and in fact it might not read any bytes at all. Whether or not it does so depends upon the nature and state of the channel. A socket channel in non-blocking mode, for example, cannot read any more bytes than are immediately available from the socket's input buffer; similarly, a file channel cannot read any more bytes than remain in the file. It is guaranteed, however, that if a channel is in blocking mode and there is at least one byte remaining in the buffer then this method will block until at least one byte is read.

> Pre-condition: ``0 <= start <= start+length <= LEN(buffer)`' and ``length >= 0`'.

> [Description inherited from Read]

> Redefines: Read

### SetBlocking

    PROCEDURE (ch: Channel) SetBlocking(block: BOOLEAN)
      RAISES Error;

> Change the channel's blocking mode. Channels typically default to blocking mode on creation.

> If a channel does not support non-blocking operation, then any call to this method is ignored. Rider implementations from IO:BinaryRider and IO:TextRider only work with blocking channels.

> [Description inherited from SetBlocking]

> Redefines: SetBlocking, SetBlocking

### Write

    PROCEDURE (ch: Channel) Write(buffer: ARRAY OF BYTE;
                    start: LONGINT;
                    length: LONGINT): LONGINT
      RAISES Error;

> Write a sequence of bytes to this channel from the buffer.

> An attempt is made to write up to length bytes to the channel. The bytes are taken from buffer, beginning at position start.

> A write operation can return after writing less than length bytes. That is, some types of channels, depending upon their state, may write only some of the bytes or possibly none at all. A socket channel in non-blocking mode, for example, cannot write any more bytes than are free in the socket's output buffer. Result is the number of bytes actually written.

> Pre-condition: ``0 <= start <= start+length <= LEN(buffer)`' and ``length >= 0`'.

> [Description inherited from Write]

> Redefines: Write

Class Detail: ServerChannel

Field Detail

### socket

    FIELD socket-: Server

Constructor Detail

### OpenServer

    PROCEDURE OpenServer(): ServerChannel
      RAISES Error;

Method Detail

### Accept

    PROCEDURE (ch: ServerChannel) Accept(): Channel
      RAISES Error;

> Accepts a connection request from a remote socket.

> On success, result is a channel for the incoming connection. If the server socket is in non-blocking mode, and there are no more pending connections, then result is NIL. An error raises an exception of type IO.Error.

### FileDescriptor

    PROCEDURE (ch: ServerChannel) FileDescriptor(): FileDescriptor

> Return the file descriptor of the channel. This is the integer number that is used to identify the channel on the level of the C library. If the channel is not associated with a file descriptor, result is ``-1`'.

> [Description inherited from FileDescriptor]

> Redefines: FileDescriptor

### SetBlocking

    PROCEDURE (ch: ServerChannel) SetBlocking(block: BOOLEAN)
      RAISES Error;

> Change the channel's blocking mode. Channels typically default to blocking mode on creation.

> If a channel does not support non-blocking operation, then any call to this method is ignored. Rider implementations from IO:BinaryRider and IO:TextRider only work with blocking channels.

> [Description inherited from SetBlocking]

> Redefines: SetBlocking