Question regarding CommStreams with protocol name 'CommTCP'.

From: Wijck, Bart van <"Wijck,>
Date: Fri, 18 May 2001 14:11:24 +0200

Hi,

I am not experienced with BlackBox. I'm trying to create a simple BlackBox
module which acts as a client and which requests a web page from a
webserver. For this purpose we use a CommStream with protocol name
'CommTCP'. The thing with which I am struggling is the way to implement the
ReadBytes for the response of the webserver.
I understand the procedures are non-blocking.
Using the module below, in procedure RequestPage I send a request for a
webpage. I'm not sure how to capture/read the response.
It looks as if procedure WriteBytes to the WebServer functions alright, but
I need an event loop (or listener or event handler).


MODULE WwebAgent;

(* WwebAgent.Try *)

IMPORT CommStreams, Out;

CONST
  CONNECT_DONE = 0;
  CONNECT_INVALID_LOCAL = 2;
  CONNECT_INVALID_REMOTE = 3;
  CONNECT_NETWORK_DOWN = 4;

TYPE
        STRING =
ARRAY OF CHAR;
        STRING128 = ARRAY 129
OF CHAR;
        STRING256 = ARRAY 266
OF CHAR;
        STRING1024 = ARRAY 1025
OF CHAR;
        
VAR
  hostaddress : STRING128;
        sHTTP :
CommStreams.Stream;
        connectstatus : INTEGER;
  pagestatus : BOOLEAN;
(*
----------------------------------------------------------------------------
--------- *)
PROCEDURE StringToByte(IN s : ARRAY OF CHAR; OUT b : ARRAY OF BYTE);
VAR
  i : INTEGER;
BEGIN
  i := 0;
  WHILE (i < LEN(s$)) & (s[i] # 0X) DO
    b[i] := SHORT(SHORT(ORD(s[i])));
    INC(i);
  END;
END StringToByte;

(*
----------------------------------------------------------------------------
--------- *)
PROCEDURE ByteToString(IN b : ARRAY OF BYTE; bytecnt : INTEGER; OUT s: ARRAY
OF CHAR);
VAR
  i : INTEGER;
BEGIN
  i := 0;
  WHILE (i < bytecnt) DO
    s[i] := CHR(b[i]);
    INC(i);
  END;
  s[i] := 0X;
END ByteToString;
        
(*
----------------------------------------------------------------------------
--------- *)
PROCEDURE Connect*(hostname : STRING; port : STRING): INTEGER;
VAR
  res : INTEGER;
BEGIN
  res := 0;

        Out.String('Connecting...');
        Out.Ln;
        hostaddress := hostname + ':' + port;
        CommStreams.NewStream ('CommTCP', '', hostaddress, sHTTP, res);
  Out.String('Connection status: ');
  Out.Int(res, 2);
  Out.Ln;
        
        RETURN res;
END Connect;

(*
----------------------------------------------------------------------------
--------- *)
PROCEDURE Close*;
BEGIN
  sHTTP.Close;
END Close;

(*
----------------------------------------------------------------------------
--------- *)
PROCEDURE RequestPage*(url : STRING): BOOLEAN;
VAR
  sRequest : STRING256;
  bRequest : ARRAY 513 OF BYTE;
  sReply : STRING1024;
  bReply : ARRAY 2049 OF BYTE;
  nrBytesWriten : INTEGER;
  nrBytesRead : INTEGER;
BEGIN
  sRequest := '';
  sReply := '';
  nrBytesWriten := 0;

        IF ~sHTTP.IsConnected() THEN
          RETURN FALSE;
        END;

  Out.String('Request');
  Out.Ln;

  sRequest := 'GET ' + url + ' HTTP/1.1\nAccept: */*\nAccept-Language:
nl\nAccept-Encoding: gzip\nUser-Agent: Mozilla/4.0 (compatible; MSIE 4.01;
Windows 95)\n'+ hostaddress;
  StringToByte(sRequest, bRequest);

        sHTTP.WriteBytes(bRequest, 0, LEN(sRequest$), nrBytesWriten);
  Out.String('Send ');
  Out.Int(nrBytesWriten, 2);
  Out.String(' bytes');
  Out.Ln;

  sHTTP.ReadBytes(bReply, 0, 1024, nrBytesRead);
  ByteToString(bReply, nrBytesRead, sReply);
  Out.String('Reply: "' + sReply + '" (');
  Out.Int(nrBytesRead, 2);
  Out.String(' bytes).');
  Out.Ln;

  RETURN TRUE;
END RequestPage;

(*
----------------------------------------------------------------------------
--------- *)
PROCEDURE Try*;
BEGIN
  connectstatus := Connect('127.0.0.1', '80');
  pagestatus := RequestPage('http://127.0.0.1/mercurius/logon.htm');
  Close();
END Try;

(*
----------------------------------------------------------------------------
--------- *)
BEGIN
END WwebAgent.

Regards,


> ____________________________________
> Bart van Wijck
> Senior Business Development Consultant
> Compuware BV
> Telefoon: 020 311 88 57
> Fax : 020 311 88 01
> Mobiel : 06 54 35 23 76
>
>
--------------------------------------------

To unsubscribe from this mailing list, send a message containing the word "unsubscribe" to:
   blackbox-request{([at]})nowhere.xy

To get a list of valid e-mail commands and instructions on their usage, send a message containing the word "help" to the above address.

Send any problem reports or questions related to this email list to the list owner at
   owner-blackbox{([at]})nowhere.xy
Received on Fri May 18 2001 - 12:11:24 UTC

This archive was generated by hypermail 2.3.0 : Thu Sep 26 2013 - 06:27:43 UTC