#include <sys/types.h> #include <sys/socket.h> int send_file(int *socket_descriptor, struct sf_parms *sf_struct, int flags)
The send_file() function is used to send the contents of an open file over an existing socket connection.
The send_file() API is a combination of the IFS read() and the sockets send() and close() APIs. Socket applications that transmit a file over a socket connection can, under certain circumstances, obtain improved performance by using send_file().
The structure pointed to by the sf_struct parameter is defined in <sys/socket.h>.
struct sf_parms { void *header_data; size_t header_length; int file_descriptor; size_t file_size; off_t file_offset; ssize_t file_bytes; void *trailer_data; size_t trailer_length; size_t bytes_sent; }
header_data | (Input/Output) A pointer to a buffer that
contains data to be sent before the file data is sent. |
header_length | (Input/Output) The length in bytes of
header_data. |
file_descriptor | (Input) The file descriptor for a file that has
been opened for read access. This is the descriptor for the file that contains
the data to be transmitted. This field is ignored if the file_bytes
field is set to 0. |
file_size | (Output) The size in bytes of the file associated
with file_descriptor. |
file_offset | (Input/Output) The byte offset into the file from
which to start sending data. Specify a value of 0 to start sending data from
the start of the file. If a negative value is passed in, send_file()
API will return with -1 and the errno will be set to EINVAL. |
file_bytes | (Input/Output) The number of bytes from the file
to be transmitted. Set the file_bytes field to -1 to transmit all of
the data from the file_offset position in the file to the end of the
file. If the file_bytes field is set to 0, no data from the file will
be transmitted. |
trailer_data | (Input/Output) A pointer to a buffer that
contains data to be sent after the file data is sent. |
trailer_length | (Input/Output) The length in bytes of
trailer_data. |
bytes_sent | (Output) The number of bytes that have been successfully sent. |
SF_CLOSE | After the header_data, file data, and trailer_data have been successfully sent, the connection and the socket descriptor are closed. The descriptor that is pointed to by the socket_descriptor parameter is set to -1 before the send_file() API returns to the application. |
SF_REUSE | After the header_data, file data, and trailer_data have been successfully sent, the connection is closed. If socket reuse is supported, the descriptor that is pointed to by the socket_descriptor parameter is reset. If socket reuse is not supported, the descriptor that is pointed to by the socket_descriptor parameter is closed and set to -1. |
No authorization is required.
send_file() returns an integer. Possible values are:
When send_file() fails, errno can be set to one of the following:
[EACCES] | Permission denied.
An attempt was made to access an object in a way forbidden by its object access permissions. A thread does not have access to the specified file, directory, component, or path. If you are accessing a remote file through the Network File System, update operations to file permissions at the server are not reflected at the client until updates to data that is stored locally by the Network File System takes place. (Several options on the Add Mounted File System (ADDMFS) command determine the time between refresh operations of local data.) Access to a remote file may also fail due to different mappings of user IDs (UID) or group IDs (GID) on the local and remote systems. |
[EBADF] | Descriptor not valid.
This error code indicates one of the following:
|
[ECONVERT] | Conversion error. |
[EFAULT] | Bad address.
The system detected an address that was not valid while attempting to access the socket_descriptor or one of the fields in the send_file structure. |
[EINTR] | Interrupted function call. |
[EINVAL] | Parameter not valid.
This error code indicates one of the following:
|
[EIO] | Input/output error. |
[ENOBUFS] | There is not enough buffer space for the
requested operation. |
[ENOTCONN] | Requested operation requires a connection. |
[ENOTSAFE] | Function is not allowed in a job that is running
with multiple threads. |
[ENOTSOCK] | The specified descriptor does not reference a
socket. |
[EOPNOTSUPP] | Operation not supported.
The socket_descriptor parameter references a socket that does not support the send_file() function. The send_file() function is only valid on sockets that have an address family of AF_INET, AF_INET6, AF_UNIX, or AF_UNIX_CCSID and a socket type of SOCK_STREAM. |
[EOVERFLOW] | Object is too large to process.
This error code indicates one of the following:
|
[EPIPE] | Broken pipe. |
[EUNATCH] | The protocol required to support the specified
address family is not available at this time. |
[EUNKNOWN] | Unknown system state. |
Message ID | Error Message Text |
---|---|
CPE3418 E | Possible APAR condition or hardware failure. |
CPF3CF2 E | Error(s) occurred during running of &1 API. |
CPF9872 E | Program or service program &1 in library &2 ended. Reason code &3. |
CPFA081 E | Unable to set return value or error code. |
CPFA0D4 E | File system error occurred. |
If O_TEXTDATA was not specified on the open() call, the data is sent from the file without conversion.
Regardless of whether or not O_TEXTDATA was specified on the open() call, the header_data and trailer_data are not translated. It is the application's responsibility to translate the header_data and trailer_data to the correct code page before calling send_file(). The send_file() function will not translate the data buffers pointed to by the header_data and trailer_data parameters prior to sending them.
Note: The ability to do code-page translation is an i5/OS specific extension to the send_file() API. The overhead to translate the file will have an effect on the performance of the send_file() API.
Note: The value that is passed in for the flags parameter is ignored if the send_file() API is interrupted by a signal.
Top | UNIX-Type APIs | APIs by category |