17. Sockets


module "/pliant/language/os/socket.pli"         # for low level socket calls
module "/pliant/language/stream/tcp.pli"        # for TCP streams
module "/pliant/protocol/common/tcp_server.pli" # for server writing

Writing a TCP Client

module "/pliant/language/stream.pli"
module "/pliant/protocol/stream/tcp.pli"
(var Stream s) open "tcp://thehost/client/port" in+out+safe
if s=failure
  console "connection failed" eol
# write a line on the stream
s writeline "Why don't you call me anymore?\n";
# read a line
var Str answer := s readline
# terminate
s close

Writing a TCP Server

module "/pliant/language/stream.pli"
module "/pliant/protocol/common/tcp_server.pli"

type MyServer
  tcp_fields "My Server" server_port
TcpServer maybe MyServer

method server service gstream
  arg_rw GeneralServer server ; arg_rw Stream stream
  # get the ip of the connected client
  var Str from_ip := gstream query "remote_ip_address"
  # handle the connection (discussion through stream)

# if you want to check some things at start time

method server start_checkup -> status
  arg_rw MyServer server ; arg Status status
  # check whatever you want
  return success

# if you want to perform actions at shutdown

method server stop_checkup
  arg_rw MyServer server
  # do whatever you need
# create a server
define_tcp_server MyServer my_server

# run the server detached
my_server detached

Communicating over TCP

Setting Up a UDP Client

module "/pliant/language/stream/stream.pli"
module "/pliant/language/stream/udp.pli"
# open connection
(var Stream s) open "udp://the_host/client/port" in+out+safe
# configure priority
s configure "priority high"
# configure timeout
s configure "timeout 10"
# write something
s writeline "hello!"
# read in buffer what is available
s read_available buffer_adr buffer_size

Setting Up a UDP Server

module "/pliant/language/stream.pli"
module "/pliant/language/stream/udp.pli"

# almost the pliant DNS UDP server implementation:

  part udp_server "UDP server" # 'part' is used for administration monitoring
    (var Stream s) open "udp:/server/53" in+out+safe
    s configure "priority high"
    while not server:please_stop_udp
      part wait_for_request "wait for UDP request"
        s read_available (var Address adr) (var Int size)  # read the request
        part answer_request "answer UDP request"
          server answer adr size s true                    # answer the request
        part reset_stream "reset UDP stream"
          s configure "reset"
        if s:is_crashed
          console "UDP stream crashed !  Restarting it." eol
          s open "udp:/server/53" in+out+safe
          s configure "priority high"
      server please_stop_udp := false

Using UNIX Domain Sockets

Identifying the Other End of a Socket

Finding Your Own Name and Address

Closing a Socket After Forking

Writing Bidirectional Clients

Forking Servers

Pre-Forking Servers

Non-Forking Servers

Writing a Multi-Homed Server

Making a Daemon Server

Restarting a Server on Demand

Program: backsniff

Program: fwdport