NAME ^

TCPStream

SYNOPSIS ^

  .sub main :main
      load_bytecode 'library/tcpstream.pir'

      .local pmc stream
      stream = new 'TCPStream'
      stream.'connect'("www.google.com", 80)

      stream.'say'("GET / HTTP/1.0")
      stream.'say'("User-agent: Parrot")
      stream.'say'("")

      $S0 = stream.'slurp'()
      print $S0

      stream.'close'()
  .end

DESCRIPTION ^

This is a high-level sockets library designed to mimic Tcl's socket builtins. As such, it uses print and say instaed of send and readline and slurp instead of recv.

VTABLE FUNCTIONS ^

init

Iniitialize a TCPStream object (create a socket and a buffer).

init_pmc

Initialize a TCPStream object from an already pre-existing socket. This can be use to create servers, among other things.

get_bool

Returns whether or not this socket has data waiting to be read.

METHODS ^

void close()

Closes the socket.

void connect(string host, int port)

Connects to a host/port.

Throws an exception if unable to connect.

void flush()

string readline()

Reads and returns up to (and excluding) an end-of-line character and discards the end-of-line character.

void say(string msg)

Print msg and an end-of-line character to the socket.

string slurp()

Return all the data in the socket.

AUTHOR ^

Matt Diephouse <matt@diephouse.com>.


parrot