source: trunk/Packages/synapse/source/demo/echo/echo.pas

Last change on this file was 2, checked in by chronos, 12 years ago
  • Přidáno: Základní kostra projektu.
  • Přidáno: Knihovna synapse.
File size: 1.7 KB
Line 
1unit echo;
2
3interface
4
5uses
6 Classes, blcksock, synsock;
7
8type
9 TTCPEchoDaemon = class(TThread)
10 private
11 Sock:TTCPBlockSocket;
12 public
13 Constructor Create;
14 Destructor Destroy; override;
15 procedure Execute; override;
16 end;
17
18 TTCPEchoThrd = class(TThread)
19 private
20 Sock:TTCPBlockSocket;
21 CSock: TSocket;
22 public
23 Constructor Create (hsock:tSocket);
24 procedure Execute; override;
25 end;
26
27implementation
28
29{ TEchoDaemon }
30
31Constructor TTCPEchoDaemon.Create;
32begin
33 inherited create(false);
34 sock:=TTCPBlockSocket.create;
35 FreeOnTerminate:=true;
36end;
37
38Destructor TTCPEchoDaemon.Destroy;
39begin
40 Sock.free;
41end;
42
43procedure TTCPEchoDaemon.Execute;
44var
45 ClientSock:TSocket;
46begin
47 with sock do
48 begin
49 CreateSocket;
50 setLinger(true,10000);
51 bind('0.0.0.0','8008');
52 listen;
53 repeat
54 if terminated then break;
55 if canread(1000) then
56 begin
57 ClientSock:=accept;
58 if lastError=0 then TTCPEchoThrd.create(ClientSock);
59 end;
60 until false;
61 end;
62end;
63
64{ TEchoThrd }
65
66Constructor TTCPEchoThrd.Create(Hsock:TSocket);
67begin
68 inherited create(false);
69 Csock := Hsock;
70 FreeOnTerminate:=true;
71end;
72
73procedure TTCPEchoThrd.Execute;
74var
75 s: string;
76begin
77 sock:=TTCPBlockSocket.create;
78 try
79 Sock.socket:=CSock;
80 sock.GetSins;
81 with sock do
82 begin
83 repeat
84 if terminated then break;
85 s := RecvPacket(60000);
86 if lastError<>0 then break;
87 SendString(s);
88 if lastError<>0 then break;
89 until false;
90 end;
91 finally
92 Sock.Free;
93 end;
94end;
95
96end.
Note: See TracBrowser for help on using the repository browser.