port_connect
lib callCapy
function port_connect(host : string, port : s32) u64
Description
port_connect() opens a TCP connection the service owns. Incoming bytes arrive as SERVICE:DATA events with port, stream set to socket, and the readable bytes. When the peer closes the connection, SERVICE:EXIT arrives with port and status.
The connection is made without blocking. A host name is resolved off the loop, and a connection or resolution that fails after the call returned arrives as SERVICE:EXIT with status -1, an error code, and a detail. Bytes queued with port_write() before the connection is up are sent once it is.
The connection stays open across events until port_close(), a peer close, or the end of the service. Use socket_connect() instead for a request-scoped connection that is read in place.
Parameters
host : a host name or address
port : the TCP port
Return Values
a port handle, or zero when the address is invalid or the connection cannot be attempted
Errors
An invalid address or a connection attempt that fails at once yields zero. A resolution or connection that fails later arrives as SERVICE:EXIT with status -1, error, and detail. Outside a service worker the call traps.
Example
Capy
var feed : u64 = 0
function SERVICE:START(props : any)
{
feed = port_connect("127.0.0.1", 11211)
port_write(feed, "version\r\n")
}
function SERVICE:DATA(props : any)
{
print(props.data)
}
function SERVICE:STOP(props : any)
{
port_close(feed)
}
Output
VERSION 1.6.40