--
--	sockstreamcli.occ - stream client
--	Copyright (C) 2000-2008 Fred Barnes <frmb@kent.ac.uk>
--      Modifications (C) 2008 Tom Carlson
--
--	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 sock.stream.cli (CHAN BYTE kyb?, scr!, err!)
PROC sock.stream.cli (CHAN BYTE kyb?, scr!, err!)

  --{{{  constants
  VAL max.message.size IS 1500:
  --}}}  

  --{{{  start here
  [max.message.size]BYTE buffer:
  INT bytes:
  SOCKET sock:
  INT res, remote.addr, remote.port:
  [128]BYTE hostname:
  INT host.len:
  SEQ
    --{{{  get hostname/port from user
    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
    out.string ("Port ?: ", 0, scr!)
    scr ! FLUSH
    in.int (remote.port, 5, kyb, scr!)
    scr ! '*n'
    --}}}  
    --{{{  create/connect socket
    socket.create.connect.tcp (sock, remote.addr, remote.port, res)
    IF
      res < 0
        SEQ
          out.string ("Unable to connect to ", 0, scr!)
          out.string ([hostname FOR host.len], 0, scr!)
          out.string (" port ", 0, scr!)
          out.int (remote.port, 0, scr!)
          out.string ("*n", 0, scr!)
          STOP
      TRUE
        SKIP
    --}}}  
    --{{{  loop sending data
    bytes, res := 100, 1
    WHILE (bytes <= 1500) AND (res > 0)
      INT i:
      SEQ
        i := 0
        out.string ("sending ", 0, scr!)
        out.int (bytes, 0, scr!)
        out.string (" byte packets...*n", 0, scr!)
        WHILE (i < 100000) AND (res > 0)
          SEQ
            socket.write (sock, [buffer FOR bytes], res)
            i := (i + 1)
        bytes := (bytes + 100)
    --}}}  
    --{{{  close socket
    socket.close (sock)
    --}}}  
  --}}}  
:
--}}}  
