Board index » delphi » Why?

Why?

I don't know sockets programming, so this can be a beginner question.

Changing the server of BasicClientServer demo to don't disconnect the client
and doing and changing the excute method like:

procedure TfrmServer.TCPServerExecute(AThread: TIdPeerThread);
begin
  inc(i);
  memo1.lines.add('execute '+inttostr(i));
  with AThread.Connection do
  begin
    WriteLn('Hello from Basic Indy Server server. The time is: ' +
FormatDateTime('hh:nn:ss',time));
//    Disconnect;
  end;
end;

where i is integer started with 0.

Why the i is incremented to some 348 in my computer and the client does not
receive 348 messages?

Bruno
Sorry for english mistakes!

 

Re:Why?


it should be initialised... ammend to:

 procedure TfrmServer.TCPServerExecute(AThread: TIdPeerThread);
 begin
   i := Memo1.lines.count;  // <----------- see here
   inc(i);
   memo1.lines.add('execute '+inttostr(i));
   with AThread.Connection do
   begin
     WriteLn('Hello from Basic Indy Server server. The time is: ' +
 FormatDateTime('hh:nn:ss',time));
 //    Disconnect;
   end;
 end;

Quote
"Bruno Lovatti" <blova...@vix.zaz.com.br> wrote in message

news:3b617b39_1@dnews...
Quote
> I don't know sockets programming, so this can be a beginner question.

> Changing the server of BasicClientServer demo to don't disconnect the
client
> and doing and changing the excute method like:

> procedure TfrmServer.TCPServerExecute(AThread: TIdPeerThread);
> begin
>   inc(i);
>   memo1.lines.add('execute '+inttostr(i));
>   with AThread.Connection do
>   begin
>     WriteLn('Hello from Basic Indy Server server. The time is: ' +
> FormatDateTime('hh:nn:ss',time));
> //    Disconnect;
>   end;
> end;

> where i is integer started with 0.

> Why the i is incremented to some 348 in my computer and the client does
not
> receive 348 messages?

> Bruno
> Sorry for english mistakes!

Re:Why?


OnExecute is execute for every client - not only for one specific.

Quote
> Why ... the client does not receive 348 messages?

Maybe it receives them but you don't read the data. You've to do ReadLn on
the client-side. In your example 348 times...

Helge.

Re:Why?


i is initialised to 0 and memo.lines.count is 0 too on FormCreate, so when
the client (only one in this test) connect and do a a readln waitting for
the server to writeln, the OnExecute Event fires 348 times counted in i and
the client only receives one message.

Bruno

Re:Why?


Thanks a lot for your reply.
Tested with one client only. I've done readln on the client and received
only one message.

Bruno

Re:Why?


Its not a sockets problem, its a problem of threads.
If a client connects to the server, the server starts a thread for this
connection.
the ...Execute IS the thread. Your first mistake is: leaving / finishing the
thread
without a 'Disconnect'.
you can do something like:

....Execute...
begin
  if connected then writeLn(<greeting>);
  while connected do readln(string var);
 or do_this
 or do_other
but finally do a disconnect
end;

the second mistake: As its a thread, you MUST NOT write to visible
components like Memo. The minimum for your tests:
In Form1 -> public:
MyString: string;
procedure ShowMyString;

in code:
procedure TForm1.ShowMyString;
begin
  Memo1.Lines.Add(MyString);
end;

in ...Execute...:
MyString := 'execute ' + IntToStr(i);
Syncronize(Form1.ShowMyString);
sleep(0); //forces task change, will help you, but not needed

later, if multiple clients will connect at the same time, you need to use
CriticalSection too.

"Bruno Lovatti" <blova...@vix.zaz.com.br> schrieb im Newsbeitrag
news:3b617b39_1@dnews...

Quote
> I don't know sockets programming, so this can be a beginner question.

> Changing the server of BasicClientServer demo to don't disconnect the
client
> and doing and changing the excute method like:

> procedure TfrmServer.TCPServerExecute(AThread: TIdPeerThread);
> begin
>   inc(i);
>   memo1.lines.add('execute '+inttostr(i));
>   with AThread.Connection do
>   begin
>     WriteLn('Hello from Basic Indy Server server. The time is: ' +
> FormatDateTime('hh:nn:ss',time));
> //    Disconnect;
>   end;
> end;

> where i is integer started with 0.

> Why the i is incremented to some 348 in my computer and the client does
not
> receive 348 messages?

> Bruno
> Sorry for english mistakes!

Re:Why?


Quote
> the ...Execute IS the thread. Your first mistake is: leaving / finishing
the
> thread without a 'Disconnect'.

Sorry. I got a little bit confused. I just checked the chat-example again
and therein the connection also not closed in OnExecute.

I understood the system as following: When the server receives data, it
fires OnExecute and gives a pointer to the corresponding thread. The event
does its work and if new data arrives the event is fired again. I have
programs working with this.

What is the right way???

Helge.

Re:Why?


Thanks!

Bruno

Re:Why?


Thanks to all replys!
I want to understand, I am not saying this is a bug or whatever, just to
learn.
For example, based on BasicClientServer demo without disconnecting the
client thread:

* The server starts
* The client (only one in the test)
* The client connects
* The server fires OnExecute 348 times (counted)
* The client does a readln and receives only one message from the server

Firstly, why OnExecute fires 348 times?
So, if it fires 348 times the OnExecute, and the
AThread.Connection.Writeln(msg) is on the OnExecute, why it doesn't send 348
messages to the client?

Bruno
Sorry for english mistakes!

Re:Why?


Quote
> When the server receives data, it
> fires OnExecute and gives a pointer to the corresponding thread.

Wrong. OnExecute is called in a tight loop within the thread, you have to make
sure that you use a blocking read-call in it.

johannes
--
Please reply in this newsgroup only
 - SIP solutions -
http://www.sipsolutions.de/

Other Threads