7 lcorewsaasyncselect in 'lcorewsaasyncselect.pas',
\r
20 procedure sessionavailable(sender: tobject;error : word);
\r
21 procedure dataavailable(sender: tobject;error : word);
\r
22 procedure sessionconnected(sender: tobject;error : word);
\r
23 procedure taskrun(wparam,lparam:longint);
\r
24 procedure timehandler(sender:tobject);
\r
25 procedure dnsrequestdone(sender:tobject;error : word);
\r
26 procedure sessionclosed(sender:tobject;error : word);
\r
29 listensocket : tlsocket;
\r
30 serversocket : tlsocket;
\r
31 clientsocket : tlsocket;
\r
34 procedure tsc.sessionavailable(sender: tobject;error : word);
\r
36 writeln('received connection');
\r
37 serversocket.dup(listensocket.accept);
\r
41 receivebuf : string;
\r
42 receivecount : integer;
\r
43 procedure tsc.dataavailable(sender: tobject;error : word);
\r
45 receiveddata : string;
\r
46 receivedon : string;
\r
48 receiveddata := tlsocket(sender).receivestr;
\r
49 if sender=clientsocket then begin
\r
50 receivedon := 'client socket';
\r
52 receivedon := 'server socket';
\r
54 writeln('received data '+receiveddata+' on '+receivedon);
\r
55 if sender=serversocket then begin
\r
56 receivebuf := receivebuf+receiveddata;
\r
58 if receivebuf = 'hello world' then begin
\r
60 writeln('received hello world creating task');
\r
61 task := tltask.create(sc.taskrun,nil,0,0);
\r
63 receivecount := receivecount +1;
\r
64 if receivecount >50 then begin
\r
65 writeln('received over 50 bits of data, pausing to let the operator take a look');
\r
72 procedure tsc.sessionconnected(sender: tobject;error : word);
\r
74 if error=0 then begin
\r
75 writeln('session is connected');
\r
76 if clientsocket.addr = '127.0.0.1' then begin
\r
77 clientsocket.sendstr('hello world');
\r
79 clientsocket.sendstr('get /'#13#10#13#10);
\r
82 writeln('connect failed');
\r
89 procedure tsc.taskrun(wparam,lparam:longint);
\r
94 writeln('task ran');
\r
95 writeln('closing client socket');
\r
98 writeln('looking up www.kame.net using dnsasync');
\r
99 das := tdnsasync.Create(nil);
\r
100 das.onrequestdone := sc.dnsrequestdone;
\r
101 das.forwardfamily := af_inet6;
102 das.forwardlookup('www.kame.net');
\r
105 procedure tsc.dnsrequestdone(sender:tobject;error : word);
\r
107 writeln('www.kame.net resolved to '+das.dnsresult+' connecting client socket there');
\r
108 clientsocket.addr := das.dnsresult;
\r
109 clientsocket.port := '80';
\r
110 clientsocket.connect;
\r
114 procedure tsc.timehandler(sender:tobject);
\r
116 //writeln('got timer event');
\r
118 procedure tsc.sessionclosed(sender:tobject;error : word);
\r
120 Writeln('session closed with error ',error);
\r
127 ipbin := forwardlookup('invalid.domain',5);
\r
128 writeln(ipbintostr(ipbin));
\r
130 ipbin := forwardlookup('p10link.net',5);
\r
131 writeln(ipbintostr(ipbin));
\r
133 ipstrtobin('80.68.89.68',ipbin);
\r
134 writeln('80.68.89.68 reverses to '+reverselookup(ipbin,5));
\r
136 ipstrtobin('2001:200::8002:203:47ff:fea5:3085',ipbin);
\r
137 writeln('2001:200::8002:203:47ff:fea5:3085 reverses to '+reverselookup(ipbin,5));
\r
138 writeln('creating and setting up listen socket');
\r
139 listensocket := tlsocket.create(nil);
\r
140 listensocket.addr := '0.0.0.0';
\r
141 listensocket.port := '12345';
\r
142 listensocket.onsessionavailable := sc.sessionavailable;
\r
143 writeln('listening');
\r
144 listensocket.listen;
\r
145 writeln('listen socket is number ', listensocket.fdhandlein);
\r
146 writeln('creating and setting up server socket');
\r
147 serversocket := tlsocket.create(nil);
\r
148 serversocket.ondataavailable := sc.dataavailable;
\r
149 writeln('creating and setting up client socket');
\r
150 clientsocket := tlsocket.create(nil);
\r
151 clientsocket.addr := {'::1';}'127.0.0.1';
\r
152 clientsocket.port := '12345';
\r
153 clientsocket.onsessionconnected := sc.sessionconnected;
\r
154 clientsocket.ondataAvailable := sc.dataavailable;
\r
155 clientsocket.onsessionclosed := sc.sessionclosed;
\r
156 writeln('connecting');
\r
157 clientsocket.connect;
\r
158 writeln('client socket is number ',clientsocket.fdhandlein);
\r
159 writeln('creating and setting up timer');
\r
160 timer := tltimer.create(nil);
\r
161 timer.interval := 1000;
\r
162 timer.ontimer := sc.timehandler;
\r
163 timer.enabled := true;
\r
164 writeln('entering message loop');
\r
166 writeln('exiting cleanly');
\r