Monks, I have a bug, and need some assistance correcting it. Here's what I'm trying to do. client sends a variable length message to server (encrypted) server reads message, does Stuff (tm) to with it and responds via the same socket. My problems is that occasionally (1/3ish of the time) the message from the client is truncated. Here are the relevant code bits:

CLIENT:

use Socket qw(:DEFAULT :crlf); use IO::Handle; my ($remote, $iaddr, $paddr, $proto, $line, $response); $remote = $client || 'localhost'; $port = $port || 8086; # random port if ($port =~ m/\D/) { $port = getservbyname($port, 'tcp') } die "Bad port" unless $port; $iaddr = inet_aton($remote) or die "can't connect: $remote"; $paddr = sockaddr_in($port, $iaddr); $proto = getprotobyname('tcp'); socket(SOCK, PF_INET, SOCK_STREAM, $proto) or die "socket: $!"; connect(SOCK, $paddr) or die "connect: $!"; SOCK->autoflush(1); print SOCK $encrypted_message . CRLF;

SERVER:

use Socket qw(:DEFAULT :crlf); use IO::Handle; next unless my $rem_addr = accept(SESSION,SOCK); my ($r_port,$r_addr) = sockaddr_in($rem_addr); warn "Connection from [", inet_ntoa($r_addr),",$r_port]\n"; SESSION->autoflush(1); my $incoming = ''; my $line = ''; while( defined($line = <SESSION>)) { $incoming .= $line; last if $incoming =~ /\n$/; # vain attempt to find end of line } chomp $incoming; my $msg = decrypt($key, $incoming);
and so on . . I can't seem to reliably detect the end of the message. Any advice would be greatly appreciated.

-anelson


In reply to Socket IO problems by anelson

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.