--
--	sockdaytime.occ - socket test to the DAYTIME port
--	Copyright (C) 2000 Fred Barnes <frmb2@ukc.ac.uk>
--
--	This program is free software; you can redistribute it and/or modify
--	it under the terms of the GNU General Public License as published by
--	the Free Software Foundation; either version 2 of the License, or
--	(at your option) any later version.
--
--	This program is distributed in the hope that it will be useful,
--	but WITHOUT ANY WARRANTY; without even the implied warranty of
--	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
--	GNU General Public License for more details.
--
--	You should have received a copy of the GNU General Public License
--	along with this program; if not, write to the Free Software
--	Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
--


#INCLUDE "sock.module"
#INCLUDE "course.module"

--{{{  PROC socktest (CHAN OF BYTE kyb, scr,err)
PROC socktest (CHAN OF BYTE kyb, scr, err)

  SOCKET sock:
  INT res:
  [256]BYTE buffer:
  INT remote.addr:
  [128]BYTE hostname:
  INT host.len:
  SEQ
    --{{{  connect to user-specified host:daytime
    out.string ("Hostname ?: ", 0, scr)
    scr ! FLUSH
    in.string (hostname, host.len, (SIZE hostname), kyb, scr)
    scr ! '*n'
    socket.addr.of.host ([hostname FOR host.len], remote.addr, res)
    IF
      res < 0
        SEQ
          out.string ("Unable to get address of ", 0, scr)
          out.string ([hostname FOR host.len], 0, scr)
          out.string ("*n", 0, scr)
          STOP
      TRUE
        SKIP

    socket.create.connect.tcp (sock, remote.addr, IPPORT.DAYTIME, res)
    IF
      res < 0
        SEQ
          out.string ("Unable to create or connect socket*n", 0, scr)
          STOP
      TRUE
        SKIP
    --}}}  
    --{{{  info about the connection
    [32]BYTE ip.addr:
    [128]BYTE host.addr:
    INT ip.len:
    INT host.len:
    SEQ
      socket.ip.of.addr (sock[remote.addr], ip.addr, ip.len, res)
      socket.host.of.addr (sock[remote.addr], host.addr, host.len, res)
      IF
        res = (-1)
          [host.addr FOR 7], host.len := "unknown", 7
        TRUE
          SKIP
      out.string ("connected to ", 0, scr)
      out.string ([host.addr FOR host.len], 0, scr)
      out.string (" (", 0, scr)
      out.string ([ip.addr FOR ip.len], 0, scr)
      out.string (") port ", 0, scr)
      out.int (sock[remote.port], 0, scr)
      scr ! '*n'
    --}}}  
    --{{{  read some data
    socket.read (sock, buffer, (SIZE buffer), res)
    IF
      res < 0
        SEQ
          out.string ("Error reading data*n", 0, scr)
          socket.close (sock)
          STOP
      res = 0
        SEQ
          out.string ("EOF from server*n", 0, scr)
          socket.close (sock)
          STOP
      TRUE
        SKIP
    --}}}  
    --{{{  print out
    out.string ("read [", 0, scr)
    WHILE (buffer[res-1] < 32)
      res := (res - 1)
    out.string ([buffer FOR res], 0, scr)
    out.string ("]*n", 0, scr)
    --}}}  
    --{{{  close connection
    socket.close (sock)
    --}}}  

:
--}}}  

