TsgcTCPClient › Methods › WriteData
Sends a text or binary message to the TCP server over the raw socket.
procedure WriteData(const aText: String; const aSize: Integer = 0; const aStreaming: TwsStreaming = stmNone);
| 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. |
aSize | const Integer | Optional fragment size in bytes; when greater than zero the payload is split into multiple chunks of this size. Use 0 (the default) to send the whole payload in a single write. |
aStreaming | const TwsStreaming | Streaming mode for fragmented transmission (stmNone, stmStart, stmContinue, stmFinish). Defaults to stmNone, which sends the payload as a single standalone write. |
WriteData transmits the string as raw bytes on the active TCP connection. Unlike the WebSocket client there is no text frame header; the peer receives exactly the bytes you send, so both ends must agree on a separator or length prefix. By default the call runs on the calling thread and returns once the bytes are handed to the socket. When QueueOptions.Text is set to a value other than qmNone, the message is queued and actually sent from the connection's own thread, which prevents locks when several threads write concurrently. Any socket exception is forwarded to OnError. Use the stream overload when sending large payloads or binary data.
When the client is not connected, nothing is sent and 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 WriteData 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 and drop the message silently, as earlier versions did. When a write fails because its connection was lost and the client has already reconnected, the failure is also reported with Connection = nil, so the new connection stays open.
oClient.WriteData('Hello Server');
procedure WriteData(const aStream: TStream; const aSize: Integer = 0; const aStreaming: TwsStreaming = stmNone);
| 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 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. |
WriteData reads the supplied stream from its current position and transmits the contents as raw bytes on the TCP socket. Because the TCP client does not wrap data in WebSocket frames, the peer must know how to delimit the incoming bytes (typically via a length prefix or an application-level separator). The call runs on the calling thread by default; when QueueOptions.Binary is set to a value other than qmNone, the message is queued and actually dispatched from the connection thread. Socket exceptions are captured and forwarded to OnError. When the client is not connected, nothing is sent and the failure is reported through OnError with Connection = nil, as described for the text overload. Free the stream only after the call returns (or, when queuing, after the send is complete).
oStream := TMemoryStream.Create;
try
oStream.LoadFromFile('payload.bin');
oClient.WriteData(oStream);
finally
oStream.Free;
end;