home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / Peter Lewis (TCPExample) / TCPExample.p next >
Encoding:
Text File  |  1996-01-17  |  1.9 KB  |  92 lines  |  [TEXT/CWIE]

  1. program UDPExample;
  2.  
  3.     uses
  4.         Events, EPPC, AppleEvents, TextUtils, 
  5.         MyInitialization, MyStartup, MyTransport, MyConnections, MyGrowZones;
  6.  
  7.     var
  8.         quitNow: Boolean;
  9.  
  10.     type
  11.         ServerObject = object(LineConnectionObject)
  12.                 procedure Established;
  13.                 override;
  14.                 procedure LineAvailable (line: Str255);
  15.                 override;
  16.             end;
  17.         MyListenerObject = object(ListenerObject)
  18.                 procedure ConnectionAvailable(tref: TransportRef);
  19.                 override;
  20.             end;
  21.         
  22.     procedure ServerObject.Established;
  23.         var
  24.             junk: OSErr;
  25.             localip: IPAddr;
  26.             localport: integer;
  27.             remoteip: longint;
  28.             remoteport: integer;
  29.     begin
  30.         junk := TransportGetPorts(tref, localip, localport, remoteip, remoteport);
  31.         writeln( 'Connection from ', IPAddrToStr( remoteip ), ':', remoteport );
  32.         SendLine( 'Hello!' );
  33.     end;
  34.     
  35.     procedure ServerObject.LineAvailable (line: Str255);
  36.     begin
  37.         if IUEqualString( line, 'quit' ) = 0 then begin
  38.             SendLine( 'Nice chatting with you');
  39.             Close;
  40.         end else begin
  41.             SendLine( 'Why do you say "' + line + '"?' );
  42.         end;
  43.     end;
  44.     
  45.     procedure MyListenerObject.ConnectionAvailable( connection: TransportRef );
  46.         var
  47.             server: ServerObject;
  48.     begin
  49.         new(server);
  50.         server.NewExistingConnection( connection );
  51.     end;
  52.     
  53.     procedure WNE;
  54.         var
  55.             junk: OSErr;
  56.             dummy:Boolean;
  57.             er:EventRecord;
  58.     begin
  59.         dummy:=WaitNextEvent(everyEvent,er,0,nil);
  60.         if er.what = keyDown then begin
  61.             quitNow := true;
  62.         end;
  63.         if er.what = kHighLevelEvent then begin
  64.             junk := AEProcessAppleEvent(er);
  65.         end;
  66.         IdleStartup;
  67.     end;
  68.  
  69.     var
  70.         err: OSErr;
  71.         msg: integer;
  72.         listener: MyListenerObject;
  73. begin
  74.     Initialization;
  75.     quitNow := false;
  76.     InitStartup;
  77.     StartupConnections;
  78.     ConfigureTransport( true );
  79.     ConfigureGrowZones( 20000 );
  80.     if Startup( msg ) = noErr then begin
  81.         new( listener );
  82.         err := listener.CreateListener( 0, 9999, 5 );
  83.         if err = noErr then begin
  84.             writeln( 'Starting' );
  85.             while not quitNow do begin
  86.                 WNE;
  87.             end;
  88.         end;
  89.         FinishStartup;
  90.     end;
  91. end.
  92.