I'd like to know if there is a way to somehow ping or send an empty request in order to see what the response from a given server will be (and it's response time) with TIdTCPClient
in delphi - what I tried was this :
TCPClient := TIdTCPClient.Create(nil);
responseStream := TMemoryStream.Create;
TCPClient.Host := someHost;
TCPClient.Port := somePort;
TCPClient.Connect;
startTime := Ticks;
TCPClient.IOHandler.ReadStream(responseStream, -1, True);
respTime := GetTickDiff(startTime, Ticks);
TCPClient.Disconnect;
In this pseudocode I successfully connect to someHost:somePort but apparently I'm not using correctly TCPClient.IOHandler.ReadStream
- can someone help me fix my code (I really have to use TIdTCPClient
) or show me how to replace the lines that aren't working properly ?
There is no such thing as an empty request in TCP (there is in UDP). If you want to send a request to a TCP server, the request has to have some data in it. Also, the particular type of server you connect to dictates the type of requests you can send to it, you cannot just connect and send whatever you want (well, you could, but that is no guarantee that you will get a response).
The way your code currently works, it will only be able to read data from servers that send an initial greeting to clients. Most servers do not do that. Your ability to get a response from a given server depends on the particular protocol implemented by that server (HTTP vs FTP vs POP3 vs SMTP vs IMAP vs ...), since it is the protocol that dictates how requests and responses work.
So as your question stands, it is not answerable because it is too generic, there are way too many possibilities. You need to narrow it down.