[fpc-pascal] fcl-net and fpasync

Xiangrong Fang xrfang at gmail.com
Mon May 18 16:48:29 CEST 2015


Hi All,

I try to write a tcp client program, and found this page:

http://pascalgeek.blogspot.com/2012/06/encryption-decryption-and-asynchronous.html

which is the only resource I found using fcl-net.

But there is a big problem: it did not tell how to receive message back
from the server.

I tried to write something like:

    EventLoop.Run;
    while true do begin
      Stream.WriteAnsiString('Hello');
      WriteLn('Server Reply: ' + Stream.ReadAnsiString);
      Sleep(1000);
    end;
    //Active := false;
And at the server side,

      Msg := FClientStream.ReadAnsiString;
      WriteLn(AddrToString(FClientStream.PeerAddress) + ': ' + Msg);
      FClientStream.WriteAnsiString('World!');   //added by me

It simply generated a runtime error: EReadError : Stream read error

As a matter of fact, I also tried to create a thread like this:

procedure TMyTCPClient.ConnStateChanged(Sender: TClientConnectionSocket;
  OldState, NewState: TConnectionState);
begin
  if NewState = connConnected then begin
    WriteLn('Connected!');
    TMsgReceiver.Create(Stream);
  end;
end;

constructor TMyTCPClient.TMsgReceiver.Create(AStream: TSocketStream);
begin
  inherited Create(False);
  FStream := AStream;
  FreeOnTerminate := True;
end;

procedure TMyTCPClient.TMsgReceiver.Execute;
var
  s: string;
begin
  while True do begin
    WriteLn('Now try to receive something...');
    s := FStream.ReadAnsiString;
    WriteLn('Received: ', s);
  end;
end;

But it did not receive anything!

How can I enable **bi-directional**, asynchronous (event driven)
communication using fcl-net, also, is there any document on fpasync??

Thanks a lot!

xrfang
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20150518/63889e41/attachment.html>


More information about the fpc-pascal mailing list