Unable to receive response from TIdTCPServer using TIdTCPClient - delphi

I want to establish a communication between TIdTCPServer and TIdTCPClient in delphi and this is how my procedures look :
1.Server side :
procedure TMainForm.IdTCPServer1Execute(AContext: TIdContext);
var
clientReq, clientName : String;
begin
clientReq := AContext.Connection.IOHandler.ReadLn(); // client sends request
clientName := extractClientName(clientReq);
AContext.Connection.IOHandler.WriteLn('Hello ' + clientName);
end;
2.Client side :
procedure TMainForm.btnTestClientClick(Sender: TObject);
var
testTCP : TIdTCPClient;
clientReq, serverResponse : String;
begin
testTCP := TIdTCPClient.Create;
try
testTCP.Host := wantedHost;
testTCP.Port := wantedPort;
testTCP.Connect;
clientReq := 'Hello, my Name is user1.';
testTCP.IOHandler.WriteLn(clientReq);
try
serverResponse := testTCP.IOHandler.ReadLn();
except on e : Exception do begin
ShowMessage('Error reading response =' + e.Message);
end;
end;
finally
FreeAndNil(testTCP);
end;
end;
I connect to the server but than my application freezes when I try to receive the response from the server OnExecute event with my TCPClient.IOHandler.ReadLn method. Can anyone help me fix my code or show me a working example of what I'm trying to do (with Indy's TIdTCPClient and TIdTCPServer) ?

There is nothing wrong with the code you have shown, so the problem has to be in the code you have not shown. The way I see it, there are two possibilities:
If you are not setting wantedHost and/or wantedPort to the correct values, you would not actually be connecting to your expected server.
If extractClientName() is getting stuck internally and not exiting, the server would not be sending any response. One way that could happen is if you are running the client and server in the same process, and extractClientName() syncs with the main thread, but the main thread is blocked waiting on the client and cannot process the sync, so a deadlock occurs.

Related

Unable to connect IdPop3 to IdPop3Server via SSL

I have a TIdPop3Server in one application that has a IdServerIOHandlerSSLOpenSSL1 attached to it and retrieves emails and sends them to a TIdPop3 client in another application (having TIdSSLIOHandlerSocketOpenSSL attached to it). Everything's fine when the connections are made insecure using port 110. But when I try to use SSL connection through port 995 I get error Connection Closed Gracefully after connect attemp from the client fails. This is my Pop3SeverOnConnect event :
procedure TMainForm.Pop3ServerConnect(AContext: TIdContext);
begin
if (AContext.Connection.IOHandler is TIdSSLIOHandlerSocketBase) then
TIdSSLIOHandlerSocketBase(AContext.Connection.IOHandler).PassThrough :=
(AContext.Binding.Port <> 995);
showmessage('SSL connection made!');
end;
And this is the client-side :
procedure TMainForm.btnCheckMailBoxClick(Sender: TObject);
begin
IdSSLIOHandlerSocketOpenSSL1.PassThrough := False;
POP3Client.IOHandler := IdSSLIOHandlerSocketOpenSSL1;
with POP3Client do begin
AuthType := patUserPass;
Host := myHost;
UserName := myUserName;
Password := myPass;
Port := myPort;
end;
try
POP3Client.Connect;
Except on e : Exception do
showmessage('error=' + e.Message);
end;
// code for retrieving message data
end;
And I always get an exception from Pop3Client.Connect like I've already mentioned above (The message SSL connection made! in the server application never shows up). If I use however another mail client like for example Mozilla Thunderbird I achieve a successful SSL connection for port 995. So the problem should be somewhere in the client's procedure but who knows - that's why I'm asking you guys for help.
In your client code, you need to set the TIdPOP3.UseTLS property instead of the TIdSSLIOHandlerSocketOpenSSL.PassThrough property directly, eg:
procedure TMainForm.btnCheckMailBoxClick(Sender: TObject);
begin
with POP3Client do
begin
IOHandler := IdSSLIOHandlerSocketOpenSSL1;
AuthType := patUserPass;
UseTLS := utUseImplicitTLS; // <-- here
Host := myHost;
UserName := myUserName;
Password := myPass;
Port := myPort;
end;
try
POP3Client.Connect;
try
// code for retrieving message data
finally
POP3Client.Disconnect;
end;
except
on e : Exception do
ShowMessage('error=' + e.Message);
end;
end;
In your server code, you need to get rid of the ShowMessage(). TIdPOP3Server is multi-threaded, the OnConnect event is fired in the context of a worker thread, and ShowMessage() is not thread-safe. If you must display a popup message, use Windows.MessageBox() instead.

Delphi Indy IRC

Delphi version : XE2,
Indy version: 10.5.8.0.
I have three procedures and all work fine until internet connection gets lost. When it will happen and after that I will try sending message then I can't reconnect when internet will be back. Can't close program (after on close program be not visible, but will use 100 cpu usage). Without "try, exception" there is a Socket Error #1053 on IdIRC1.Say and on Close. Thanks for help.
///Connection:
procedure TForm1.Button5Click(Sender : TObject);
begin
try
IdIRC1.Nickname := 'zzz';
IdIRC1.Password := 'kkk';
if IdIRC1.Connected then
IdIRC1.Disconnect;
IdIRC1.Connect;
IdIRC1.Join('#' + edit3.Text);
except
ShowMessage('ggg');
end;
end;
///Send message:
procedure TForm1.Button3Click(Sender : TObject);
begin
try
IdIRC1.Say('#' + edit3.Text, edit2.Text);
if (edit2.Text <> '') and (IdIRC1.Connected) then
begin
memo6.Lines.Add(edit2.Text);
Edit2.Clear;
end
else
ShowMessage('xxx');
except
ShowMessage('yyy');
end;
end;
///On close:
try
IdIRC1.Disconnect;
except
end;
When you encounter an error accessing the connection, such as because the connection was lost, you need to call Disconnect() and you need to clear the IOHandler.InputBuffer if it still has unread data in it. Disconnect() does not clear the InputBuffer, by design. If the InputBuffer is not empty, Connected() will return True even if the physical socket is disconnected.

Indy10 TCP and asynchronous data exchange

Good morning to all.I am building a Delphi TCP server/client application using Indy 10.0.52,with TIdTCPClient and TIdTCPServer. I have problem with receiving asynchronous responses from server. Here's my part of sample code:
Client:
procedure TfrmMain.ClientSend();
begin
tcpClient.IOHandler.WriteLn('100|HelloServer');
if tcpClient.IOHandler.ReadLn() = '1100' then //Here I get 1100
begin
tcpClient.IOHandler.WriteLn('200|Are_you_ready_for_data?');
if tcpClient.IOHandler.ReadLn() = '1200' then
begin
end;
end;
end;
Server:
procedure TfrmMain.tcpServerExecute(AContext: TIdContext);
var command:Integer;
var received,value:String;
begin
received := AContext.Connection.IOHandler.ReadLn;
command := StrToInt(SomeSplitFunction(received,'first_part')); //Here I get 100
value := SomeSplitFunction(received,'second_part'); //Here I get 'HelloServer'
case command of
100:begin
//Do something with value...
AContext.Connection.IOHandler.WriteLn('1100');
end;
200:begin
//Do something with value...
AContext.Connection.IOHandler.WriteLn('1200');
end;
end;
end;
The problem is that the case 200 on tcpServerExecute is never executed, therefore the second ReadLn on client site is never read.Is multiple asynchronous data sending in single procedure supported?I have came across several examples with simple Indy TCP Server/Client applications, but I'm little stuck here.Just to mention that connection is working and I connect to server without problems.

Delphi (Indy) Clients sends server request, how to wait for response?

I have just managed to get the client (IdTCPClient) to send a message to the server (IdTCPServer) as required. But how do I get the client to wait for a response, or time out appropriately?
Cheers,
Adrian
The client can read the response with the IOHandler.Readxxx methods, most of them allow to set a timeout. The read timeout can also be specified on the IdTCPClient.IOHandler directly.
procedure TForm1.ReadTimerElapsed(Sender: TObject);
var
S: String;
begin
...
// connect
IdTCPClient1.Connect;
// send data
...
// use one of the Read methods to read the response.
// some methods have a timeout parameter,
// and others set a timeout flag
S := IdTCPClient1.IOHandler.ReadLn(...);
if IdTCPClient1.IOHandler.ReadLnTimedOut then
...
else
...
end;
See also: How can I wait for a string from a server with IdTCPClient?
For example:
Client:
procedure TForm1.SendCmdButtonClick(Sender: TObject);
var
Resp: String;
begin
Client.IOHandler.WriteLn('CMD');
Resp := Client.IOHandler.ReadLn;
end;
Server:
procedure TForm1.IdTCPServer1Execute(AContext: TIdContext);
var
Cmd: String;
begin
Cmd := AContext.Connection.IOHandler.ReadLn;
...
AContext.Connection.IOHandler.WriteLn(...);
end;
Alternatively, you can use the TIdTCPConnection.SendCmd() method instead:
Client:
procedure TForm1.SendCmdButtonClick(Sender: TObject);
begin
// any non-200 reply will raise an EIdReplyRFCError exception
Client.SendCmd('CMD', 200);
// Client.LastCmdResult.Text will contain the response text
end;
Server:
procedure TForm1.IdTCPServer1Execute(AContext: TIdContext);
var
Cmd: String;
begin
Cmd := AContext.Connection.IOHandler.ReadLn;
...
if (Command is Successful) then
AContext.Connection.IOHandler.WriteLn('200 ' + ...);
else
AContext.Connection.IOHandler.WriteLn('500 Some Error Text here');
end;
In this latter scenario, if you switch to TIdCmdTCPServer, you can use the TIdCmdTCPServer.CommandHandlers collection to define your commands at design-time and assign per-command OnCommand event handlers to them, instead of using the OnExecute event to read and parse the commands manually, eg:
// OnCommand event handler for 'CMD' TIdCommandHandler object...
procedure TForm1.IdCmdTCPServer1CMDCommand(ASender: TIdCommand);
begin
...
if (Command is Successful) then
ASender.Reply.SetReply(200, ...);
else
ASender.Reply.SetReply(500, 'Some Error Text here');
end;
It's been a while since I've used the Indy components (or Delphi for that matter) but I believe that the TIdTCPClient doesn't behave asynchronously so there's no OnData or similar event that you can set.
You will need to call one of the read methods from the parent class (TIdTCPConnection) such as ReadLn(...). Alternatively you could look at using one of the many Indy components that are TIdTCPClient descendants.
Documentation for the class can be found here.

Send command to all connected clients

I have a TIdHttpServer i must keep the connection open in order to send some commands back to the clients. I want to iterate when i press a button and send a command to all connected clients.
How can i do this ?
You can use the Contexts property to get the clients and then using the IOHandler of each client you can send a message.
Var
Clients : TList;
i : integer;
begin
if not Assigned(IdTCPServer1.Contexts) then exit;
Clients:=IdTCPServer1.Contexts.LockList;
try
for i := 0 to Clients.Count-1 do
try
TIdContext(Clients[i]).Connection.IOHandler.Write(LBuffer);//LBuffer is a TBytes with the data to send
except
...
end;
finally
IdTCPServer1.Contexts.UnlockList;
end;
end;

Resources