Hi guys, bit of a quick, and possibly obvious one (but hey, i'm stuck so why not ask!), the code below was derived from an example I found on here, when you first connect tcp_connect is aware of the port you're comming in on, the connections will all be persistent so I want to use that port to differentiate between connections, the problem is I can't seem to find away to determine the port from within on_read!
use warnings; use AnyEvent; use AnyEvent::Socket; use AnyEvent::Handle; use Data::Dumper; my $port = 7777; my $cv = AnyEvent->condvar; tcp_server undef, 7777, sub { my ( $fh, $host, $port ) = @_; warn "Accepted connection from $host:$port\n"; create_handle( $fh, 0 ); }; sub create_handle { my ( $socket, $exit_on_close ) = @_; my ( $shandle, $thandle ); my $destroy = sub { $shandle->destroy; $cv->send if $exit_on_close; }; $shandle = AnyEvent::Handle->new( fh => $socket, on_error => $destroy, on_close => $destroy, ); $shandle->on_read( sub { if ( $shandle->rbuf eq "marco" ) { $shandle->push_write("polo"); } print "A\n"; $shandle->rbuf = ''; print $shandle->rbuf; } ); $shandle->on_eof(undef); } $cv->recv;

In reply to Getting port number with AnyEvent::Socket by MiggyMan

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.