We help IT Professionals succeed at work.

TTcpClient problem cant recieve anything

T0masz
T0masz asked
on
Medium Priority
1,291 Views
Last Modified: 2010-05-18
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;
Comment
Watch Question

var
  lResult: string;
begin
  TcpClient1.Sendln('F');
  lResult:=TcpClient1.ReadLn;
end;

Author

Commented:
I want it to be read as data comes in (onevent) not have to wait after sending something each time

Tom

Author

Commented:
oh and tcpclient1 doesnt have a readln function
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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
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
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.