Sun Java System Web Server 7.0 Update 4 NSAPI Developer's Guide

net_sendfile() Function

The net_sendfile function sends the contents of a specified file to a specified a socket. Either the whole file or a fraction might be sent, and the contents of the file might optionally be preceded or followed by caller-specified data.

Parameters are passed to net_sendfile in the sendfiledata structure. Before invoking net_sendfile, the caller must initialize every sendfiledata structure member.


int net_sendfile(SYS_NETFD sd, const sendfiledata *sfd);

Return Values

A positive number indicating the number of bytes successfully written, including the headers, file contents, and trailers. A negative value indicates an error.


SYS_NETFD sd is the socket to write to.

const sendfiledata *sfd identifies the data to send.


The following Service SAF sends a file bracketed by the strings "begin" and "end."

#include <string.h>
#include "nsapi.h"

NSAPI_PUBLIC int service_net_sendfile(pblock *pb, Session *sn, Request *rq)
    char *path;
    SYS_FILE fd;
    struct sendfiledata sfd;
    int rv;

    path = pblock_findval("path", rq->vars);
    fd = system_fopenRO(path);
    if (!fd) {
        log_error(LOG_MISCONFIG, "service-net-sendfile", sn, rq,
                  "Error opening %s (%s)", path, system_errmsg());
        return REQ_ABORTED;

    sfd.fd = fd;                     /* file to send */
    sfd.offset = 0;                  /* start sending from the beginning */
    sfd.len = 0;                     /* send the whole file */
    sfd.header = "begin";            /* header data to send before the file */
    sfd.hlen = strlen(sfd.header);   /* length of header data */
    sfd.trailer = "end";             /* trailer data to send after the file */
    sfd.tlen = strlen(sfd.trailer);  /* length of trailer data */

    /* send the headers, file, and trailers to the client */
    rv = net_sendfile(sn->csd, &sfd);


    if (rv < 0) {
        log_error(LOG_INFORM, "service-net-sendfile", sn, rq,
                 "Error sending %s (%s)", path, 
        return REQ_ABORTED;

    return REQ_PROCEED;

See Also

net_flush() Function