Question

I am trying to build a simple client - server - client application using the IdHTTP component. How to use IdHttp component to send array of bytes from a client1 to a php file located on www.example.com and have this php file send the same data to be consumed by a client2 using the IdHTTp? Maybe I am taking the wrong path here.

Was it helpful?

Solution

For sending the data from client1 to the server I recommend to use Base64 encoding. This way you have a normal string instead of a byte array. Sending strings with IdHTTP is simple. An alternative is multipart form data (Java code here).

For receiving the data on client2: HTTP clients do not know when there is data on the server. Asynchronous receive (server push) is a feature of the new WebSocket protocol, which is an extension of HTTP so it needs specifix extensions on the client and server side. There are open source and commercial implementations of the WebSocket protocol for Delphi.

If your client2 is a normal HTTP client, it has no option than to continuously poll data from the server. For basic use cases, I would recommend a TIdTCPClient component instead of TidHTTP which gives you more control over the processing of incoming data. The client jst needs to open a socket connection on port 80 of the server, send a well-formed HTTP request and then run a loop to receive the response. When the server has new data, the PHP script will start to send data (maybe even without HTTP response headers) and then the Base64 encoded data.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top