ghopwood has asked for the wisdom of the Perl Monks concerning the following question:

I have a problem that I'm scratching my head on. I have a client that does the following:
Connects to TCP port 2000.
Sends the string below.
Disconnects
[01]Title[02][1C]payload[03]number[04] [01] = ASCII SOH [02] = ASCII STX [1C] = ASCII FS [03] = ASCII ETX [04] = ASCII EOT
Using the netcat command,

# netcat -l 2000

I see the data being sent on port 2000 as it should be.

However, I can not get any of my Perl TCP code to detect that there is something arriving on the socket.

Here are two examples I'm using.
use strict; use IO::Socket; my $sock = new IO::Socket::INET ( LocalPort => '2000', Proto => 'tcp', Listen => 5, Reuse => 1, ); die "Could not create socket: $!\n" unless $sock; my $new_sock = $sock->accept(); while(<$new_sock>) { print $_; } close($sock);
use strict; use POE qw(Component::Server::TCP); POE::Component::Server::TCP->new( Port => 2000, ClientInput => \&event_tcp_incoming, ); sub event_tcp_incoming { my ($heap, $kernel, $input) = @_[HEAP, KERNEL, ARG0]; print "Received: $input\n"; }
Neither of these examples ever 'detect' or 'see' something on the socket to read.

Replies are listed 'Best First'.
Re: TCP Socket not receiving.
by BrowserUk (Patriarch) on Jul 24, 2012 at 15:52 UTC
    while(<$new_sock>) {

    You are using readline to read from your socket. That means that it will read bytes until it sees a byte or bytes that match the current value of $/ (the input separator), which by default is set to "\n".

    For networking purposes, a newline is defined to be ascii(13-decimal) + ascii(10-decimal)

    As your data doesn't contain a newline character, those reads continue to wait.

    If you redefined the input separator to be EOT (ie. $/ = chr(4);), then your code would probably work as expected.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    The start of some sanity?

      That was exactly it!

      You know in the back of my mind I had this nagging suspicion it was lack of the return\line feed.

      Its been so long since I've coded in Perl (or not enough coffee), I just couldn't put it into a solidly formed idea, or find it jumping out at me in the docs.

      Heres the working code. Thanks.
      #!/usr/bin/perl -w use strict; use POE qw(Component::Server::TCP); POE::Component::Server::TCP->new( Alias => 'linda_server', Port => 2000, Concurrency => 5, ClientConnected => \&event_tcp_conn, ClientDisconnected => \&event_tcp_disconn, ClientInput => \&event_tcp_incoming, ClientFilter => [ "POE::Filter::Line", Literal => chr(4) ], ); print "Starting POE Kernel.\n"; $poe_kernel->run(); sub event_tcp_conn { my ($heap,$kernel) = @_[HEAP, KERNEL]; print "Connection: $heap->{remote_ip} Accepted\n"; } sub event_tcp_disconn { my ($heap,$kernel) = @_[HEAP, KERNEL]; print "Disconnect: $heap->{remote_ip}.\n"; } sub event_tcp_incoming { my ($heap, $kernel, $input) = @_[HEAP, KERNEL, ARG0]; print "Received: $heap->{remote_ip} : $input\n"; } __END__
Re: TCP Socket not receiving.
by zwon (Abbot) on Jul 24, 2012 at 15:47 UTC
    Your first example works for me just fine.

    Update: I didn't pay attention to the data you're sending. See BrowserUk's reply for solution.