• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1226
  • Last Modified:

TTcpClient problem cant recieve anything

Hey, Im having a problem where I connect to 127.0.0.1 on port 666 using the function in button1 then onconnect i get a message saying it connects just fine. Now button2 sends the character 'F' and its supposed to get a bunch of data back. Well the TcpClient1Recieve function never gets called... why?

if i do
telnet 127.0.0.1 666
F
GOT:F

it works just fine.

Now what am i doing wrong? and please dont tell me to work around it by using indy or whatever else.
Tom

procedure TForm1.Button1Click(Sender: TObject);
begin
TcpClient1.Connect
end;

procedure TForm1.TcpClient1Receive(Sender: TObject; Buf: PAnsiChar;
  var DataLen: Integer);
begin
       showmessage('GOTDATABACK');
end;

procedure TForm1.TcpClient1Connect(Sender: TObject);
begin
          ShowMessage('Connected');
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
        TcpClient1.Sendln('F');
end;
0
T0masz
Asked:
T0masz
  • 2
  • 2
2 Solutions
 
pcsentinelCommented:
var
  lResult: string;
begin
  TcpClient1.Sendln('F');
  lResult:=TcpClient1.ReadLn;
end;
0
 
T0maszAuthor Commented:
I want it to be read as data comes in (onevent) not have to wait after sending something each time

Tom
0
 
T0maszAuthor Commented:
oh and tcpclient1 doesnt have a readln function
0
 
pcsentinelCommented:
The way indy works is that a client invokes an action on a server, because of this the client has no direct way of activating when it receives data. You can get round this by using a timer

set up a timer, say 500ms and then on the timer do

this if for INDY 9

var
 lMsg: string;
begin
  Timer1.Enabled:=false;
  lMsg:=idTCPClient1.ReadLn;
  if lMsg<>'' then
  begin
   {process your message here}
  end;
  Timer1.Enabled:=true;
end;



for Indy 10 which I haven't got installed I think you need


var
 lMsg: string;
begin
  Timer1.Enabled:=false;
  lMsg:=idTCPClient1.IOHandler.ReadLn;
  if lMsg<>'' then
  begin
   {process your message here}
  end;
  Timer1.Enabled:=true;
end;

regards
0
 
MainiacfreakusCommented:
try this:

procedure TForm1.Timer1Timer(Sender: TObject);
begin
  If idtcpclient1.Connected then Begin
    IdTCPClient1.CheckForGracefulDisconnect(True);
    If not IdTCPClient1.IOHandler.InputBufferIsEmpty then ShowMessage(IdTCPClient1.IOHandler.ReadLn);
  End;
end;

NB: It uses a timer event to check if there is data to read, if there is, it reads it...

Mainiacfreakus
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now