Start of trail stream_send_array navigation bar

Table of Contents

stream_send

Sends an arbitrary data type over a stream.

Syntax

[items_sent, would_block] = stream_send(stream, values)
    

Description

This function writes data to the stream buffer. It attempts to store the values array in the stream buffer according to its data type. If there is enough room available in the stream buffer then it stores the data in the buffer and returns immediately. The data is not written to the actual communication channel until the stream is flushed using stream_flush or there is no more room available in the stream buffer. If an error occurs, then it issues an error message. If the connection is closed it is considered an error condition.

If stream_listen or stream_connect was called with the non-blocking flag set to false (0), then this function may block attempting to flush the stream buffer. All the data will be consumed and the total number of items sent is returned. Some of the data may remain in the stream buffer and not be sent until the next time stream_flush is called or there is no more room available in the stream buffer. If an error occurs then an error is issued and the stream should be closed.

If stream_listen or stream_connect was called with the non-blocking flag set to true (1), then this function does not block. It returns the number of items sent successfully, which will be between 1 and the number of elements in the values array (unless it is empty). If no items could be sent without blocking, then would_block is set to true (1). If an error occurs then an error is issued and the stream should be closed.

Note that each element of the values array is sent as an atomic unit. Hence, this function will always send complete elements, but it may not send the entire array if non-blocking I/O is used. To ensure that the entire array is always sent as an atomic unit, use the stream_send_array function instead.

This operation is not valid on listening streams created using stream_listen.

Parameters

stream

A stream handle returned by stream_connect or stream_accept.

values

The values to send. The values may be numeric, logical, characters, cell arrays or structures.

Outputs

items_sent

The number of items actually written to the stream buffer, which may be less than the number requested for non-blocking streams. For blocking streams, a value of 0 is returned only if the values array is empty.

would_block

Whether the function would have blocked. Always false for blocking streams.

Examples

        s.x = 3.14159;
        s.y = 2.71828;
        s.z = int16(172);
        num_items = stream_send(stream, s);    % Send structure as two doubles followed by one 16-bit integer
    

See Also

 

navigation bar