# Io Reference

Networking / Socket / Socket

Interface to network communication. Sockets will auto yield to other coroutines while waiting on a request. All blocking operations use the timeout settings of the socket. Reads are appended to the socket's read buffer which can be accessed using the readBuffer method. Example:

    socket := Socket clone setHost("www.yahoo.com") setPort(80) connect
    if(socket error) then( write(socket error, "\n"); exit)

    socket write("GET /\n\n")

    while(socket read, Nop)
    if(socket error) then(write(socket error, "\n"); exit)

    write("read ", socket readBuffer length, " bytes\n")

acceptTimeout

Returns the length of time in seconds for accept timeouts on the socket.
appendToWriteBuffer(aSequence)

Appends aSequence to the write buffer if it is non-nil. Returns self.
asyncAccept(ipAddressObject)

Immediately returns a socket for a connection if one is available or nil otherwise. Returns an Error object on error.
asyncBind

Binds the socket and returns self immediately or an Error object on error.
asyncConnect(ipAddressObject)

Connects to the given IPAddress and returns self or an Error object on error.
asyncListen

Listens to the socket and returns self immediately or an Error object on error.
asyncStreamOpen

Submits an async request to open the socket in stream mode and returns self immediately or an Error object on error.
asyncStreamRead(aSeq, readSize)

Reads up to readSize number of bytes into aSeq if data is available. Returns self immediately if successful. Returns an error object on Error. Returns nil if the socket is disconnected.
asyncStreamWrite(aSeq, start, writeSize)

Writes the slice of aSeq from start to start + writeSize to the socket. Returns self immediately if successful, otherwise closes the socket. Returns an error object on Error. Returns nil if the socket is disconnected.
asyncUdpOpen

Submits an async request to open the socket in UDP mode and returns self immediately or an Error object on error.
asyncUdpRead(ipAddress, aSeq, readSize)

Reads up to readSize number of bytes from ipAddress into aSeq if data is available. Returns self immediately if successful. Returns an error object on Error. Returns nil if the socket is disconnected.
asyncUdpWrite(ipAddress, aSeq, startIndex, readSize)

Writes readsize bytes from aSeq starting at startIndex to ipAddress. Returns self immediately if successful. Returns an error object on Error. Returns nil if the socket is disconnected.
bytesPerRead

Returns number of bytes to read per read call.
bytesPerWrite

Returns number of bytes to write per write call.
close

Closes the socket and returns self. Returns nil on error.
connect

Connects to the socket's host. Returns self on success or an Error object on error.
connectTimeout

Returns the length of time in seconds for connect timeouts on the socket.
descriptorId

Returns the socket's file descriptor id as a Number.
errorDescription

Returns a description of the last error on the socket as a string.
errorNumber

Returns the socket error number for the last error.
getSocketReadLowWaterMark

Returns the read low water mark for the socket on success or nil on error.
getSocketWriteLowWaterMark

Returns the write low water mark for the socket on success or nil on error.
host

Returns the host for the socket.
ipAddress

Returns the IpAddress object for the socket.
isOpen

Returns true if the socket is open, false otherwise.
isStream

Returns true if the socket is a stream, false otherwise.
isValid

Returns true if the socket is in valid state, closes the socket and returns false otherwise.
port

Returns the port number for the socket.
readBytes(numBytes)

Reads the socket until its readBuffer is numBytes long, then returns a Sequence containing the first numBytes of readBuffer's contents and clips that section from the readBuffer.
readListMessage

A shortcut for List fromEncodedList(socket readMessage).
readMessage

Empties the readBuffer and reads a 4 byte uint32 in network byte order. This number is the number of bytes in the message payload which are then read into the socket's readBuffer. The readBuffer is returned.
readTimeout

Returns the length of time in seconds for read timeouts on the socket.
readUntilSeq(aSequence)

Reads the socket until its readBuffer contains aSequence, then returns a Sequence containing the readBuffer's contents up to (but not including) aSequence and clips that section from the readBuffer.
serverOpen

Opens the socket as a stream, binds it to its ipAddress and calls asyncListen to prepare the socket to accept connections. Returns self on success or an Error object on error.
serverWaitForConnection

Waits for a connection or timeout. When a connection is received, this method returns the connection socket. An Error object is returned on timeour or error.
setAcceptTimeout(seconds)

Sets the length of time in seconds for accept timeouts on the socket. Returns self.
setBytesPerRead(numberOfBytes)

Sets number of bytes to read per read call. Returns self.
setBytesPerWrite(numberOfBytes)

Sets number of bytes to write per write call. Returns self.
setConnectTimeout(seconds)

Sets the length of time in seconds for connect timeouts on the socket. Returns self.
setHost(hostName)

Translates hostName to an IP using asynchronous DNS and sets the host attribute. Returns self.
setHost(hostNameOrIpString)

Set the host for the socket. Returns self on success, an Error object otherwise.
setIpAddress(ipAddressObject)

Sets the ipAddress for the socket. Returns self. The setHost() method should generally be used to set the host instead of this method.
setNoDelay

Sets the socket to be no-delay. Returns self on success or nil on error.
setPort(portNumber)

Sets the port number for the socket, returns self.
setReadTimeout(seconds)

Sets the length of time in seconds for read timeouts on the socket. Returns self.
setSocketReadBufferSize(numberOfBytes)

Sets the read buffer size for the socket. Returns self on success or nil on error.
setSocketReadLowWaterMark(numberOfBytes)

Sets the read low water mark for the socket. Returns self on success or nil on error.
setSocketWriteBufferSize(numberOfBytes)

Sets the write buffer size for the socket. Returns self on success or nil on error.
setSocketWriteLowWaterMark(numberOfBytes)

Sets the write low water mark for the socket. Returns self on success or nil on error.
setWriteTimeout(seconds)

Sets the length of time in seconds for write timeouts on the socket. Returns self.
streamOpen

Opens the socket in stream mode. Returns self.
streamRead(numberOfBytes)

Reads numberOfBytes from the socket into the socket's readBuffer. Returns self when all bytes are read or an Error object on error.
streamReadNextChunk(optionalProgressBlock)

Waits for incoming data on the socket and when found, reads any available data and returns self. Returns self on success or an Error object on error or timeout.
streamReadWhileOpen

Reads the stream into the socket's readBuffer until it closes. Returns self on success or an Error object on error.
streamWrite(buffer, optionalProgressBlock)

Writes buffer to the socket. If optionalProgressBlock is supplied, it is periodically called with the number of bytes written as an argument. Returns self on success or an Error object on error.
udpOpen

Opens the socket in UDP (connectionless) mode. Returns self.
udpRead(ipAddress, numBytes)

Waits for and reads numBytes of udp data from the specified ipAddress into the socket's readBuffer. Returns self on success or an Error object on error.
udpReadNextChunk(ipAddress)

Waits to receive UDP data from the specified ipAddress. As soon as any data is available, it reads all of it into the socket's readBuffer. Returns self on success or an Error object on error.
udpWrite

Same as asyncUdpWrite.
writeFromBuffer(optionalProgressBlock)

Writes the contents of the socket's writeBuffer to the socket. If optionalProgressBlock is supplied, it is periodically called with the number of bytes written as an argument. Returns self on success or an Error object on error.
writeListMessage(aList)

A shortcut for writeMessage(aList asEncodedList).
writeMessage(aSeq)

Writes a 4 byte uint32 in network byte order containing the size of aSeq. Then writes the bytes in aSeq and returns self.
writeTimeout

Returns the length of time in seconds for write timeouts on the socket.