Skip to main content
Home Forums 68kMLA Help with TCPExample in Pascal — #10
Post #10 by mactjaap
Source Forum68kMLA
CategoryDevelopment
Post DateTue, 22 Oct 2013 - 22:07
Original URLhttps://68kmla.org/bb/threads/help-with-tcpexample-in-pascal.29110/
Post
YES!!!! This is it!! Thanks a lot!

I just added the values 0 for remotehost and remote port and nil and then it works.

This is my code now. Please do understand that I'm not much of a programmer, but trying hard to do my best! Some variables are still in it but only relevant for NewActiveConnection.

Code:
program MacTjaapExampleCode;

{ You can do anything you want with this code - if you can make any money out of it, you'll be doing well! }

uses
 TCPTypes, TCPStuff, TCPConnections;

const
 user_name = 'GET / HTTP/1.0';
 dest_name = 'www.apache.org';
 dest_port = 80;
 nul = chr(0);
 lf = chr(10);
 cr = chr(13);
 localport = 80;
 remotehost = 0;
 remoteport = 0;
 dataptr = 0;



var
 oe: OSErr;
 cp: connectionIndex;
 quitNow: boolean;
 cer: connectionEventRecord; { Event record for TCP events, simmilar to EventRecord }
 s: str255;
 count: longInt;
 gotlinefeed: boolean;
begin
ShowText;
quitNow := false;
oe := InitConnections; { Startup the TCP units }
if oe = noErr then begin
 oe := FindAddress(cp, dest_name, nil);
 if oe = noErr then begin
  count := 0;
  while not quitNow do begin
   if GetConnectionEvent(any_connection, cer) then begin { Get the next TCP event }
    case cer.event of
     C_Found:  begin






{function NewPassiveConnection (var cp: connectionIndex; buffersize: longInt; localport: integer; remotehost: longInt; remoteport: integer; dataptr: univ ptr): OSErr;}

      oe := NewPassiveConnection(cp, Default_TCPBUFFERSIZE, localport, remotehost, remoteport, nil);
     end;
     C_SearchFailed:  begin
      writeln('Couldn''t fine the address for ', dest_name);
      quitNow := true;
     end;
     C_Established:  begin { Happens once per succesful connection establishment }
      writeln('Connection Established');
      s := concat(user_name, cr, lf, cr, lf); { Send the line that finger wants, complete with crlf }
{oe := TCPSend(cer.tcpc, @s[1], length(s), true);}
      writeln('Name sent with error ', oe);
      if oe <> noErr then
      CloseConnection(cp); { Better close the connection if we can't send anything to it! }
     end;
     C_FailedToOpen: 
      writeln('Ooops, connection failed to open... Error is ', cer.value, ' Timed out is ', cer.timedout);
{ Example, network unreachable etc.  Error code is in cer.value }
     C_Closing:  begin
      writeln('Connection closing');{ Gets called when the connection starts closing down}
      CloseConnection(cer.connection); { Close our side of the connection }
     end;
     C_Closed:  begin
      writeln('Connection closed, quit now');
      quitNow := true; { The connection is closed, quit the program }
     end;
     C_CharsAvailable:  begin
{$PUSH}
{$R-}
      oe := TCPReceiveUpTo(cer.tcpc, 10, 60, @s[1], 255, count, gotlinefeed);
{ Recieve characters up to a line feed }
      if (count > 0) & (s[count] = lf) then { strip off linefeed }
      count := count - 1;
      if (count > 0) & (s[count] = cr) then { strip off cr }
      count := count - 1;
      s[0] := chr(count);
{$POP}
      if gotlinefeed then begin { if we got a linefeed, print the string, otherwise go round again and wait for more characters }
      writeln(s);
      count := 0;
      end;
     end;
    end;
   end;
  end;
 end;
 FinishEverything; { Close everything, clean up }
{ ALWAY CALL THIS, OR YOU WILL BE SORRY! }
end;
writeln('Click to quit');
while not Button do
 ;
end.
It now listens on port 80 (web server port) and recieves the GET request of a browser. This is shown in the textbox of the Mac where my TCPExample is running.

NewPassiveConnection.JPG

Working example listening on port 80. Will show you the browser call in a text box.

TCPExample80.zip

Next challenge..... I will try to send something back to the browser. Will I need this function?

 

function TCPSend (connection: TCPConnectionPtr; writePtr: ptr; writeCount: integer; push: boolean): OSErr;

mp.ls