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 | |
---|
1 | unit echo;
|
---|
2 |
|
---|
3 | interface
|
---|
4 |
|
---|
5 | uses
|
---|
6 | Classes, blcksock, synsock;
|
---|
7 |
|
---|
8 | type
|
---|
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 |
|
---|
27 | implementation
|
---|
28 |
|
---|
29 | { TEchoDaemon }
|
---|
30 |
|
---|
31 | Constructor TTCPEchoDaemon.Create;
|
---|
32 | begin
|
---|
33 | inherited create(false);
|
---|
34 | sock:=TTCPBlockSocket.create;
|
---|
35 | FreeOnTerminate:=true;
|
---|
36 | end;
|
---|
37 |
|
---|
38 | Destructor TTCPEchoDaemon.Destroy;
|
---|
39 | begin
|
---|
40 | Sock.free;
|
---|
41 | end;
|
---|
42 |
|
---|
43 | procedure TTCPEchoDaemon.Execute;
|
---|
44 | var
|
---|
45 | ClientSock:TSocket;
|
---|
46 | begin
|
---|
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;
|
---|
62 | end;
|
---|
63 |
|
---|
64 | { TEchoThrd }
|
---|
65 |
|
---|
66 | Constructor TTCPEchoThrd.Create(Hsock:TSocket);
|
---|
67 | begin
|
---|
68 | inherited create(false);
|
---|
69 | Csock := Hsock;
|
---|
70 | FreeOnTerminate:=true;
|
---|
71 | end;
|
---|
72 |
|
---|
73 | procedure TTCPEchoThrd.Execute;
|
---|
74 | var
|
---|
75 | s: string;
|
---|
76 | begin
|
---|
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;
|
---|
94 | end;
|
---|
95 |
|
---|
96 | end.
|
---|
Note:
See
TracBrowser
for help on using the repository browser.