TransmitFile

Notice This function is a Microsoft-specific extension to the Windows Sockets specification. For more information, see Microsoft Extensions and Windows Sockets 2.

The Windows Sockets TransmitFile function transmits file data over a connected socket handle. This function uses the operating system's cache manager to retrieve the file data, and provides high-performance file data transfer over sockets.

BOOL TransmitFile(
  SOCKET hSocket,                             
  HANDLE hFile,                               
  DWORD nNumberOfBytesToWrite,                
  DWORD nNumberOfBytesPerSend,                
  LPOVERLAPPED lpOverlapped,                  
  LPTRANSMIT_FILE_BUFFERS lpTransmitBuffers,  
  DWORD dwFlags                               
);
 

Parameters

hSocket
A handle to a connected socket. The function will transmit the file data over this socket.

The socket specified by hSocket must be a connection-oriented socket.

Sockets of type SOCK_STREAM, SOCK_SEQPACKET, or SOCK_RDM are connection-oriented sockets. The TransmitFile function does not support datagram sockets.

hFile
A handle to an open file. The function transmits this file's data. The operating system reads the file data sequentially. You can improve caching performance by opening the handle with the FILE_FLAG_SEQUENTIAL_SCAN.
nNumberOfBytesToWrite
The number of bytes to transmit. The function completes when it has sent this many bytes, or if an error occurs.

Set this parameter to zero to transmit the entire file.

nNumberOfBytesPerSend
The size of each block of data sent per send operation. This specification is for use by the sockets layer of the operating system.

Set this parameter to zero to have the sockets layer select a default send size.

This parameter is useful for message protocols that have limitations on the size of individual send requests.

lpOverlapped
Pointer to an OVERLAPPED structure. If the socket handle has been opened as overlapped, specify this parameter in order to achieve an overlapped (aysnchronous) I/O operation. By default, socket handles are opened as overlapped.

You can use lpOverlapped to specify an offset within the file at which to start the file data transfer by setting the Offset and OffsetHigh member of the OVERLAPPED structure. If lpOverlapped is NULL, the transmission of data always starts at the current byte offset in the file.

When lpOverlapped is not NULL, the overlapped I/O might not finish before TransmitFile returns. In that case, the TransmitFile function returns FALSE, and GetLastError returns ERROR_IO_PENDING. This lets the caller continue processing while the file transmission operation completes. The operating system will set the event specified by the hEvent member of the OVERLAPPED structure, or the socket specified by hSocket, to the signaled state upon completion of the data transmission request.

lpTransmitBuffers
Pointer to a TRANSMIT_FILE_BUFFERS data structure that contains pointers to data to send before and after the file data is sent. Set this parameter to NULL if you only want to transmit the file data.
dwFlags
An attribute that has three settings:
TF_DISCONNECT
Start a transport-level disconnect after all the file data has been queued for transmission.
TF_REUSE_SOCKET
Prepare the socket handle to be reused. When the TransmitFile request completes, the socket handle can be passed to the AcceptEx function. It is only valid if TF_DISCONNECT is also specified.
TF_WRITE_BEHIND
Complete the TransmitFile request immediately, without pending. If this flag is specified and TransmitFile succeeds, then the data has been accepted by the system but not necessarily acknowledged by the remote end. If TransmitFile returns TRUE, there will be no completion port indication for the I/O. Do not use this setting with the other two settings.

Return Values

If the TransmitFile function succeeds, the return value is TRUE. Otherwise, the return value is FALSE. To get extended error information, call GetLastError. The function returns FALSE if an overlapped I/O operation is not complete before TransmitFile returns. In that case, GetLastError returns ERROR_IO_PENDING.

Remarks

The Windows NT Server optimizes the TransmitFile function for high performance. The Windows NT Workstation optimizes the function for minimum memory and resource utilization. Expect better performance results when using TransmitFile on Windows NT Server.

QuickInfo

  Windows NT: Yes
  Windows CE: Unsupported.
  Header: Declared in mswsock.h.
  Import Library: Link with mswsock.lib.