[previous][up][top][index]
search for:

using sockets

It's easy to use sockets as though they were files. Simply replace the file name by a string of the form $host:service where host is the name of IP number of host to contact, and service is the port number or name to use. If service is omitted, then port 2500 is used. If host is omitted, then an incoming connection will be listened for.

For the demonstration, we use fork to create a separate process which will listen for a connection on port 7500 and then send us a message.

i1 : if (pid = fork()) == 0 then (
          try "$:7500" << "hi there" << close;
          exit 0;
          )

Let's wait for a while to make sure the child process is listening.

i2 : sleep 2

o2 = 0

Now we can use an ordinary input command like get to obtain the entire message.

i3 : get "$localhost:7500"

o3 = hi there

Finally, we can wait for the child process to finish.

i4 : wait pid

o4 = 0

See also:

  • openInOut -- open an input outpuf file
  • openListener -- open a port for listening

  • [previous][up][top][index]
    search for: