I am writing a server in Perl to run on a Windows system for POSIX clients. The server accepts HTML from incoming connections, returning a Word document for the response. It requires a Windows system with Office and Services for UNIX (Perl w/OLE), as well as socat or similar bi-directional socket tool on the clients. The server works about half the time. The rest of the time, the connection dies early without an error on either side. The output is the same whether or not the connection dies early.


On the Windows system:
C:\Documents and Settings\Administrator>\SFU\Perl\bin\Perl html2doc-server.pl
Server startup in 9 seconds on tcp/7422
New connection
3 1024-byte input buffers processed
Open temporary c:\SFU\tmp\s8o.0.html
Write temporary c:\SFU\tmp\s8o.0.html.doc
24 1024-byte output buffers processed
Done with connection
New connection
3 1024-byte input buffers processed
Open temporary c:\SFU\tmp\s8o.10.html
Write temporary c:\SFU\tmp\s8o.10.html.doc
24 1024-byte output buffers processed
Done with connection

On the POSIX system:
$ cat input.html | socat STDIO TCP4:windows:7422 > output.doc
$ file output.doc
output.doc: Microsoft Office Document
$ cat input.html | socat STDIO TCP4:windows:7422 > output.doc
$ file output.doc
output.doc: empty

html2doc-server.pl
use IO::Socket; use Win32::OLE qw(in with); use Win32::OLE::Const 'Microsoft Word'; use Win32::OLE::Variant; use IO::Handle; use POSIX; ## This is a server that accepts HTML documents on port ## 7422, returning a Word document. As far as I know, it ## can only be run with ActiveState ActivePerl on Windows ## with Microsoft Word installed. Also, if using SFU, ## must run from C:\SFU\Perl\bin\Perl.exe, not the ## POSIX version at C:\SFU\usr\local\bin\perl. The POSIX ## version has no support for Win32::OLE. If anyone hacks ## OLE support into the POSIX version, let me know at: ## <inittab@unixdev.net>. my $server_port = 7422; my $current_time = time(); my $word; eval {$word = Win32::OLE->GetActiveObject('Word.Application')}; die "Word not installed" if $@; unless (defined $word) { $word = Win32::OLE->new('Word.Application', sub { $_[0]->Quit; }) or die "Cannot start Word"; } Win32::OLE->Option(Warn => 3); $server = IO::Socket::INET->new(LocalPort => $server_port, Proto => 'tcp', Type => SOCK_STREAM, Reuse => 1, Listen => 5) or die "Could not open server on tcp/$server_port +: $@\n"; print STDERR "Server startup in ", time() - $current_time, " seconds o +n tcp/$server_port\n"; while ($client = $server->accept()) { print STDERR "New connection\n"; ###TODO: GET REAL TEMPORARY FILENAME (this is hit-and-miss) my $file = "c:\\SFU\\tmp" . POSIX::tmpnam() . "0.html"; ###RECEIVE HTML FROM CLIENT open(IFILE, ">$file") or next; #TODO: log error my $i = 0; my $buf = ""; while (read($client, $buf, 1024, 0) > 0) { print IFILE $buf; $i++; } print STDERR "$i 1024-byte input buffers processed\n"; undef $i; undef $buf; close(IFILE); print STDERR "Open temporary $file\n"; #TODO: if verbose my $doc = $word->{'Documents'}->Open("$file") or next; #TODO: log +error print STDERR "Write temporary $file.doc\n"; #TODO: if verbose $doc->SaveAs("$file.doc", { 'FileFormat' => wdFormatDocument }); $doc->Close(); undef $doc; ###SEND DOC BACK TO CLIENT open(OFILE, "<$file.doc") or die "Could not open Office Document"; + #TODO: log error binmode(OFILE); my $buf = ""; my $i = 0; while (read(OFILE, $buf, 1024, 0) > 0) { print $client $buf; $i++; } print STDERR "$i 1024-byte output buffers processed\n"; undef $i; undef $buf; close(OFILE); ###REMOVE TEMPORARY HTML & DOC unlink("$file") or print STDERR "ERROR: could not delete $file\n"; unlink("$file.doc") or print STDERR "ERROR: could not delete $file +.doc\n"; $client->close; print STDERR "Done with connection\n"; } undef $word; close($server);

In reply to html2doc server connection dies early by IdleResonance

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.