ptg10805159
836 Communicating with a Network Printer Chapter 21
725 /*
726 * Write the headers first. Then send the file.
727 */
728 iov[0].iov_base=hbuf;
729 iov[0].iov_len=hlen;
730 iov[1].iov_base=ibuf;
731 iov[1].iov_len=ilen;
732 if (writev(sockfd, iov, 2) != hlen + ilen) {
733 log_ret("can’t write to printer");
734 goto defer;
735 }
736 if (jp->req.flags & PR_TEXT) {
737 /*
738 * Hack: allow PostScript to be printed as plain text.
739 */
740 if (write(sockfd, "\b", 1) != 1) {
741 log_ret("can’t write to printer");
742 goto defer;
743 }
744 }
745 while ((nr = read(fd, buf, IOBUFSZ)) > 0) {
746 if ((nw = writen(sockfd, buf, nr)) != nr) {
747 if (nw < 0)
748 log_ret("can’t write to printer");
749 else
750 log_msg("short write (%d/%d) to printer", nw, nr);
751 goto defer;
752 }
753 }
[725 – 735] We set the first element of theiovecarray to refer to the HTTP header and
the second element to refer to the IPP header.Then we usewritevto send
both headers to the printer.Ifthe write fails or we write less than we
requested, we log a message and jump todefer,where we will clean up
and delay beforetrying again.
[736 – 744] Even if we specify plaintext, the Phaser 8560 will try to autosense the
document format.To prevent it from recognizing the beginning of a file we
want to print as plaintext, the first character we send is a backspace. This
character doesn’t show up in the printout and defeats the printer ’s ability to
autosense the file format. This allows us to print the source to a PostScript
file instead of printing the image resulting from the PostScript file.
[745 – 753] We send the data file to the printer inIOBUFSZchunks. writecan send
less than we requested when the socket buffers arefull, so we usewritento
handle this case. We don’t worry about this condition when we write the
headers, because they aresmall. However,the file to print could be large.