<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
</head>
<body bgcolor="#FFFFFF" text="#000000">
Hi, I'm using Synapse to make a TCP server. Does someone know why
the function CanRead return true when the server thread terminates?
In the synapse echo demo, this causes a new ClientThread to be
created on closing the app or any time the thread is terminated. I
cant figure out why this should happen?<br>
<br>
To avoid that I changed:<code class="bbc_code"><br>
<br>
if CanRead(1000) then</code><br>
<br>
to :<code class="bbc_code"><br>
<br>
if CanRead(1000) and (not Terminated) then</code><br>
<br>
Execute of server thread (from synapse echo demo):<br>
<br>
procedure TTCPEchoDaemon.Execute;<br>
var<br>
ClientSock: TSocket;<br>
begin<br>
with Sock do<br>
begin<br>
CreateSocket;<br>
SetLinger(true,10000);<br>
Bind('127.0.0.1','7070');<br>
Listen;<br>
repeat<br>
if Terminated then Break;<br>
if CanRead(1000) then<br>
begin<br>
ShowMessage('The thread is dead, long live the
thread.'); //Exception on thread terminate<br>
ClientSock:= Accept;<br>
if LastError = 0 then<br>
begin<br>
TTCPEchoThrd.Create(ClientSock);<br>
end;<br>
end;<br>
until False;<br>
end;<br>
end;<br>
<br>
<br>
<br>
<br>
<br>
<br>
It causes the program to go on and create a new ClientThread even
when Terminated is true. After understanding that this happened, I
worked around it by changing:<br>
<br>
<div class="codeheader">Code: <a href="javascript:void(0);"
onclick="return smfSelectText(this);" class="codeoperation">[Select]</a></div>
<pre style="margin: 0; padding: 0;"><code class="bbc_code"> if CanRead(1000) then</code></pre>
<br>
to this:<br>
<br>
<div class="codeheader">Code: <a href="javascript:void(0);"
onclick="return smfSelectText(this);" class="codeoperation">[Select]</a></div>
<pre style="margin: 0; padding: 0;"><code class="bbc_code"> if CanRead(1000) and (not Terminated) then</code></pre>
<br>
But is that good? Shouldnt something like that be in one of the
demos if so? Or I have not understood how it should be done?<br>
<br>
<pre style="margin: 0; padding: 0;"><code class="bbc_code">procedure TMyServer.Execute;
var
ClientSock: TSocket;
aClientThread: TClientThread;
begin
with Sock do
begin
CreateSocket; // auto-created by Synapse if not done manually}
SetLinger(True, 10000);
Bind('0.0.0.0','7070');
Listen;
if LastError = 0 then Log('Server is Online');
repeat
if Terminated then
begin
if Assigned(aClientThread) then aClientThread.Terminate;
Break;
end;
if CanRead(1000) and (not Terminated) then //Neglect CanRead if serverthread is terminated!
begin
Log('Server: Someone connected');
ClientSock := Accept;
if LastError = 0 then
begin
aClientThread:= TClientThread.Create(ClientSock);
aClientThread.OnLog:= Self.OnLog; //Enable logging to main form
aClientThread.Start;
end;
end;
until False;
end;
end;</code></pre>
<br>
<br>
<br>
Also, from comments in Synapse sources:<br>
<br>
"CanRead: This function is need only on special cases, when you need
use<br>
@link(RecvBuffer) function directly! read functioms what have
timeout as<br>
calling parameter, calling this function internally."<br>
<br>
<br>
</body>
</html>