Here's my problem. I have to make an interactive client that receives and process incoming that. The trick part -where I'm stuck with- is that each transaction is enclose like this STXxxxxxxxxETX... no new line character, ever. My prototype works fine with the new line character... but I can't figure out how to read and process each transaction as it arrives. Here is my code: The server
#!/usr/bin/perl
#telcel server

$ETX = chr(03);
$STX=chr(03);

use IO::Socket;
use Sys::Hostname;
my $sock = new IO::Socket::INET(
                   LocalHost => 'localhost',
                   LocalPort =>10019,
                   Proto     => 'tcp',
                   Listen    => SOMAXCONN,
                   Reuse     => 1);
$sock or die "no socket :$!";
STDOUT->autoflush(1);

$|=1;


my($new_sock, $buf);

# print "$buf\n";

while ($new_sock = $sock->accept()) {
    # got a client connection, so read
    # line by line until end-of-file
# print "BUF: $buf";






    while (defined($buf = <$new_sock>)) {
#    while (defined($buf = split(/$ETX/,<$new_sock>))) {




   foreach ($buf) {
#                       chop($buf);
                        $buf =~ s/98/99/gi;
                        print($new_sock "$buf"),
                        print "$buf\n";

   }
    }
    close $new_sock;
}

The client
#!/usr/bin/perl
# client2way.pl - a client that writes to
# and reads from a server


require "./cgiCommon";


use IO::Socket;

#my $host = shift || '10.252.1.41';
$host = shift || 'localhost';
#my $port = shift || 10018;
my $port = shift || 10019;

my $sock = new IO::Socket::INET(
                  PeerAddr => $host,
                  PeerPort => $port,
                  Proto    => 'tcp');
$sock or die "no socket :$!";
$sock->autoflush(1);


use DBI;

if ($sock){
print "Connected to $host\n\n";
}


$counter="0";

while ($counter < 30){
# send message to server
#print $sock "HELLO\n";
# print server response to STDOUT

&Dates;

&get_id;
$transaction_id= "AM" . sprintf("%06d",$id);

$string= chr(02) . "98" . $transaction_id . $fecha . $hora . chr(03) . "\n";
#$string= chr(02) . "98" . $transaction_id . $fecha . $hora . chr(03);

print $sock "$string" or die;

print "REQ: $string\n";

&save_echo ($string);

$response = scalar <$sock>;

print "RES: $response\n";

&save_echo ($response);



sleep(10);
$counter++;


In reply to Socket IO NO new line by indigo1tx

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.