Reputation:
I have on exe which is I run on my local machine (127.0.0.1). This exe writes at port 1234 and reads at 5678. exe writes after every 50 seconds an integer value say 1212, 4545. 6767 etc. I want to read that integer value and display. So I am using Indy Client to serve the purpose. I have developed following code snippet for that.
IdTCPClient1.Port := 1234; //Set port to connect to
IdTCPClient1.Host := '127.0.0.1'; //Set host to connect to
IdTCPClient1.Connect; //Make connection
sMsg := IdTCPClient1.Socket.ReadLn; //Read the response from the server
ShowMessage(sMsg);
But its not reading. While debugging it gets stuck at line (sMsg := IdTCPClient1.Socket.ReadLn;)
When I try to do this by using telnet command like this telnet 127.0.0.1 1234 some miscellaneous or special characters are displayed after regular intervals not the integer values which server sends.
Please suggest any solution for this.
Upvotes: 2
Views: 2893
Reputation: 17203
I'm not sure about where resides your problem, so, I'm posting a full example to create a basic Indy Server and Client applications.
First, I have a server application with a IdTCPServer
component and a button. Relevant properties are:
object Button1: TButton
Text = 'Listen'
OnClick = Button1Click
end
object IdTCPServer1: TIdTCPServer
DefaultPort = 1234
OnExecute = IdTCPServer1Execute
end
and the IdTCPServer.OnExecute
and Button.OnClick
method on the server looks like this:
procedure TServerForm.Button1Click(Sender: TObject);
begin
IdTCPServer1.Active := not IdTCPServer1.Active;
if IdTCPServer1.Active then
Button1.Text := 'Close'
else
Button1.Text := 'Listen';
end;
procedure TServerForm.IdTCPServer1Execute(AContext: TIdContext);
var
Num: Integer;
begin
while (IdTCPServer1.Active) and (AContext.Connection.Connected) do
begin
Num := Random(MaxInt);
AContext.Connection.IOHandler.WriteLn(IntToStr(Num));
Sleep(1000);
end;
end;
As you can see, for each connected client, we will enter a loop where each second will be written a random number (as string) to the socket.
I execute the server and press the button to start listening, accept the Firewall warning to allow the port open and then I can successfully connect and get information from this server via telnet:
Now, I created the client application.
A Button, Memo and IdTCPClient on the form, relevant properties are:
object Button1: TButton
Text = 'Connect'
OnClick = Button1Click
end
object Memo1: TMemo
end
object IdTCPClient1: TIdTCPClient
Host = 'localhost'
Port = 1234
end
and the code looks like this:
procedure TClientForm.ReadResults;
var
S: string;
begin
while IdTCPClient1.Connected do
begin
S := IdTCPClient1.IOHandler.ReadLn;
Memo1.Lines.Add(S);
//don't repeat this approach in production code, it's just a test here
Application.ProcessMessages;
end;
end;
procedure TClientForm.Button1Click(Sender: TObject);
begin
if IdTCPClient1.Connected then
begin
IdTCPClient1.Disconnect;
Button1.Text := 'Connect';
end
else
begin
IdTCPClient1.Connect;
Button1.Text := 'Disconnect';
Button1.Repaint;
ReadResults;
end;
end;
At runtime it looks like this:
The project is made in FireMonkey with Delphi XE3, but it should work also with VCL with any Delphi version that supports Indy 10.
Upvotes: 4