TsgcTCPClient › Methods › WriteAndWaitData
Sends a message and blocks the caller until the server responds with a matching reply or the timeout elapses.
function WriteAndWaitData(const aText: String; const aTimeout: Integer = 10000): string;
| Name | Type | Description |
|---|---|---|
aText | const String | The text payload to send to the server. The bytes are written straight to the TCP socket without any framing header. |
aTimeout | const Integer | Maximum time in milliseconds to wait for the server reply. Defaults to 10000 (10 seconds). |
The text message received from the server, or an empty string when the timeout expired, no connection was available, or an error occurred. (string)
WriteAndWaitData is a convenience helper for request/response style exchanges over a plain TCP socket. It writes the string as raw bytes and then blocks the calling thread on an internal event until the next inbound text payload arrives or the timeout fires. Because the TCP client does not frame messages, the peer must return a self-delimited reply that the component can surface through OnMessage. Because the call blocks, it should not be invoked from the main UI thread unless a short timeout is used. An empty string is returned when the server does not reply in time; exceptions at the socket level are trapped and forwarded to OnError. Use the stream overload when you need to send and receive binary payloads.
When the client is not connected, WriteAndWaitData sends nothing and returns an empty string immediately instead of waiting for the timeout. The failure is reported through OnError with Connection = nil and the error Data not sent, the client is not connected or its handshake has not completed. (resourcestring S_ERROR_CLIENT_NOT_CONNECTED). The event fires on the calling thread before the method returns, and LastError holds the same text. No exception is raised to the caller and the client is not disconnected. Set Options.RaiseDisconnectExceptions to False in code to skip this report.
vReply := oClient.WriteAndWaitData('REQUEST', 5000);
if vReply <> '' then
ShowMessage(vReply);
function WriteAndWaitData(const aStream: TStream; const aSize: Integer = 0; const aStreaming: TwsStreaming = stmNone; const aTimeout: Integer = 10000) : TStream;
| Name | Type | Description |
|---|---|---|
aStream | const TStream | Source stream whose contents will be written to the socket as raw bytes. The caller remains the owner of the stream. |
aSize | const Integer | Optional fragment size in bytes; when greater than zero the outgoing stream is split into multiple chunks of this size. Use 0 (the default) to send the whole stream in a single write. |
aStreaming | const TwsStreaming | Streaming mode for fragmented transmission (stmNone, stmStart, stmContinue, stmFinish). Defaults to stmNone, which sends the stream as a single standalone write. |
aTimeout | const Integer | Maximum time in milliseconds to wait for the server reply. Defaults to 10000 (10 seconds). |
A newly allocated stream containing the binary reply received from the server, or nil when the timeout expired, no connection was available, or an error occurred. The caller is responsible for freeing the returned stream. (TStream)
This overload is the binary counterpart to the text overload. It writes the supplied stream as raw bytes on the TCP socket and then blocks the calling thread on an internal event until the next inbound binary payload arrives via OnBinary or the timeout fires. Because the TCP client does not wrap data in frames, the peer must send a self-delimited reply that the component can deliver as a discrete message. Because the call blocks, avoid invoking it from the main UI thread with a long timeout. The return value is nil when the server does not reply in time; exceptions are trapped and forwarded to OnError. When the client is not connected, the method returns nil immediately without waiting for the timeout and reports the failure through OnError with Connection = nil, as described for the text overload. Free both the source stream (after the call) and the returned stream when you are done with them.
oRequest := TMemoryStream.Create;
try
oRequest.LoadFromFile('request.bin');
oReply := oClient.WriteAndWaitData(oRequest, 0, stmNone, 5000);
try
if Assigned(oReply) then
oReply.SaveToFile('reply.bin');
finally
oReply.Free;
end;
finally
oRequest.Free;
end;