program lcoretest; uses lcore, lsocket, {$ifdef win32} lcorewsaasyncselect in 'lcorewsaasyncselect.pas', {$else} lcoreselect, {$endif} dnsasync, binipstuff, dnssync; {$ifdef win32} {$R *.RES} {$endif} type tsc=class procedure sessionavailable(sender: tobject;error : word); procedure dataavailable(sender: tobject;error : word); procedure sessionconnected(sender: tobject;error : word); procedure taskrun(wparam,lparam:longint); procedure timehandler(sender:tobject); procedure dnsrequestdone(sender:tobject;error : word); procedure sessionclosed(sender:tobject;error : word); end; var listensocket : tlsocket; serversocket : tlsocket; clientsocket : tlsocket; sc : tsc; task : tltask; procedure tsc.sessionavailable(sender: tobject;error : word); begin writeln('received connection'); serversocket.dup(listensocket.accept); end; var receivebuf : string; receivecount : integer; procedure tsc.dataavailable(sender: tobject;error : word); var receiveddata : string; receivedon : string; begin receiveddata := tlsocket(sender).receivestr; if sender=clientsocket then begin receivedon := 'client socket'; end else begin receivedon := 'server socket'; end; writeln('received data '+receiveddata+' on '+receivedon); if sender=serversocket then begin receivebuf := receivebuf+receiveddata; end; if receivebuf = 'hello world' then begin receivebuf := ''; writeln('received hello world creating task'); task := tltask.create(sc.taskrun,nil,0,0); end; receivecount := receivecount +1; if receivecount >50 then begin writeln('received over 50 bits of data, pausing to let the operator take a look'); readln; receivecount := 0; end; end; procedure tsc.sessionconnected(sender: tobject;error : word); begin if error=0 then begin writeln('session is connected'); if clientsocket.addr = '127.0.0.1' then begin clientsocket.sendstr('hello world'); end else begin clientsocket.sendstr('get /'#13#10#13#10); end; end else begin writeln('connect failed'); end; end; var das : tdnsasync; procedure tsc.taskrun(wparam,lparam:longint); var tempbinip : tbinip; dummy : integer; begin writeln('task ran'); writeln('closing client socket'); clientsocket.close; writeln('looking up www.kame.net using dnsasync'); das := tdnsasync.Create(nil); das.onrequestdone := sc.dnsrequestdone; das.forwardfamily := af_inet6; das.forwardlookup('www.kame.net'); end; procedure tsc.dnsrequestdone(sender:tobject;error : word); begin writeln('www.kame.net resolved to '+das.dnsresult+' connecting client socket there'); clientsocket.addr := das.dnsresult; clientsocket.port := '80'; clientsocket.connect; das.free; end; procedure tsc.timehandler(sender:tobject); begin //writeln('got timer event'); end; procedure tsc.sessionclosed(sender:tobject;error : word); begin Writeln('session closed with error ',error); end; var timer : tltimer; ipbin : tbinip; dummy : integer; begin ipbin := forwardlookup('invalid.domain',5); writeln(ipbintostr(ipbin)); ipbin := forwardlookup('p10link.net',5); writeln(ipbintostr(ipbin)); ipstrtobin('80.68.89.68',ipbin); writeln('80.68.89.68 reverses to '+reverselookup(ipbin,5)); ipstrtobin('2001:200::8002:203:47ff:fea5:3085',ipbin); writeln('2001:200::8002:203:47ff:fea5:3085 reverses to '+reverselookup(ipbin,5)); writeln('creating and setting up listen socket'); listensocket := tlsocket.create(nil); listensocket.addr := '0.0.0.0'; listensocket.port := '12345'; listensocket.onsessionavailable := sc.sessionavailable; writeln('listening'); listensocket.listen; writeln('listen socket is number ', listensocket.fdhandlein); writeln('creating and setting up server socket'); serversocket := tlsocket.create(nil); serversocket.ondataavailable := sc.dataavailable; writeln('creating and setting up client socket'); clientsocket := tlsocket.create(nil); clientsocket.addr := {'::1';}'127.0.0.1'; clientsocket.port := '12345'; clientsocket.onsessionconnected := sc.sessionconnected; clientsocket.ondataAvailable := sc.dataavailable; clientsocket.onsessionclosed := sc.sessionclosed; writeln('connecting'); clientsocket.connect; writeln('client socket is number ',clientsocket.fdhandlein); writeln('creating and setting up timer'); timer := tltimer.create(nil); timer.interval := 1000; timer.ontimer := sc.timehandler; timer.enabled := true; writeln('entering message loop'); messageloop; writeln('exiting cleanly'); end.