Link to home
Start Free TrialLog in
Avatar of Roger Alcindor
Roger Alcindor

asked on

How to Accept a TCP connection using Indy 10 TIdTCPServer

I need some assistance in writing a TCP Server in Embarcadero C++ builder XE 10.1 using  an Indy 10 TCP server VCL control.
How do I accept a connection from a client when using an Indy 10 VCL control TIdTCPServer ?
I imagine that I need to write some code in the OnConnect Event handler although logically one might expect a "BeforeConnect"  event handler .I haven't found any methods anywhere in the AContext or its members called Accept() ?
Also, my understanding is that  when a connection is established, the Execute event is invoked which supplies an AContext parameter which equates to a thread. Can I let the Execute event handler terminate and have a separate thread that manipulates the saved value of the AContext parameter in a list. the separate thread would poll round each AContext in the list  to independently send/receive data to/from the remote clients ?
ASKER CERTIFIED SOLUTION
Avatar of sarabande
sarabande
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Roger Alcindor
Roger Alcindor

ASKER

I was thinking that there was a function to prevent the connection whereas it's clear that the TCP connection has already been made when the Connect event handler is invoked so the code in the event handler is to Close the connection rather than preventing it in the first place.
Something like:

void __fastcall TForm1::IdTCPServer1Connect(TIdContext *AContext)
{
   	AContext->Connection->Socket->CloseGracefully();
}

Open in new window


Thanks for your help,

Roger.
Hello Sara, Do you have any comments relating to the second part of my question ?
unfortunately I am not familiar with details of TIdTCPServer, but generally a TCP/IP server listens to a bound port number and calls accept what means it was waiting for clients to connect. this normally happens with a timeout such that the server or the thread wasn't blocked. if a client connects the server would  return from accept call and gets a new socket handle as return value. this socket can be used to exchange messages with the client or to cancel the connection by closing the connection. if you close the connection (gracefully or not) you can't create a new connection to the client beside that the client is a server as well (what rarely is the case).

so, actually, you should use the connection that was passed with the context structure. and as told, the accept already has happened and you should find both a thread and a socket of the client connection which you can use to communicate with the client.

I can check the docs if you get stuck. but any server-client implementation will follow those principals as they have to use the tcp-ip subsystem of the operation system where the API design is standardized across platforms.

Sara
Thanks for your last comment, I shall percevere.

Roger