Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: POE TCP client problem

by rcaputo (Chaplain)
on Jan 07, 2010 at 19:16 UTC ( [id://816167]=note: print w/replies, xml ) Need Help??


in reply to POE TCP client problem

You're using POE very incorrectly. Based on the reading of your server, here's one that should do what you want. It's not tested at all, however. People in irc.perl.org #poe may be able to help you further.

#!/usr/bin/perl #use strict; # use both POE server and the filter needed use POE qw(Component::Server::TCP Filter::Stream); my $Dest = Value("DestinationXMLLocation"); my $Src = Value("SourceXMLLocation"); POE::Component::Server::TCP->new( Alias => "sum_server", Port => 11211, ClientFilter => "POE::Filter::Stream", ClientInput => sub { my ($storage, $client_input) = @_[HEAP, ARG0]; $storage->{buf} .= $client_input; # Shift the next request off the buffer. my ($req_type, $next_request, $new_buf) = parse_client_input( $storage->{buf} ); # No request yet. Wait for more input. return unless defined $next_request; # Replace the buffer with what's left. $storage->{buf} = $new_buf; if ($req_type eq "interface_levels_available") { open(FH, "<", "$Src\\interfaceLevelsAvailableResponse-OK.xml") o +r die $!; } elsif ($req_type eq "interface_level_request") { open(FH, "<", "$Src\\interfaceLevelResponse-OK.xml") or die $!; } else { warn "ignoring illegal request: $next_request\n"; return; } # Send the contents of the opened file. my $output = join("", <FH>); close(FH); $storage->{client}->put($output); }, ); sub parse_client_input { my $input_buffer = shift; # Up to you to define. # Removes one request from the beginning of $input_buffer. # Returns nothing if $input_buffer doesn't contain a complete reques +t. # Returns the new request type, the request itself, and the remains # of $input_buffer on success. # # If your protocol is line-based, use POE::Filter::Line above # instead of POE::Filter::Stream, and then don't worry about buffer # management. my $request_type = "interface_levels_available"; return($request_type, $input_buffer, ""); }

Replies are listed 'Best First'.
Re^2: POE TCP client problem
by baldeep8 (Initiate) on Jan 08, 2010 at 07:21 UTC
    Hi rcaputo,

    Thanks a lot for the reply. I have already tried using POE like the way you have suggested. I have used Client Input on the server side and Server Input along with Connected components on the client side. My problem on the Client side is that i am not able to synchronize between sending and receiving files. We do a put ie a send operation in the Connected part and do a receive ie a heap input operation on the Server Input part. But how do we then synchronize if i want to first write a file then read then again write and then read again. This requires some kind of synchronization between Server Input and Connected components reads and writes. I am not sure how to do this. Please help.

    #!/usr/bin/perl -w use strict; use POE; use POE::Component::Client::TCP; use POE::Filter::Reference; my $host = "localhost"; # The host to test. my $port = 11212; my @values = (6, 2); POE::Component::Client::TCP->new( RemoteAddress => $host, RemotePort => $port, Filter => "POE::Filter::Reference", Connected => sub { my $j = "teste"; print "connected to $host:$port ...\n"; $_[HEAP]->{server}->put(\@values); }, ConnectError => sub { print "could not connect to $host:$port ...\n"; }, ServerInput => sub { #when the server answer the question my ($kernel, $heap, $input) = @_[KERNEL, HEAP, ARG0]; print "got result from $host:$port ... YAY!\n"; #print to screen the result print $$input. "\n"; }, ); $poe_kernel->run(); exit 0;

      I suspect I misunderstand your protocol. Based on my reading of your sample code, the timing seems to be:

      • Client sends a file to the server in response to establishing a successful connection.
      • Server receives the client's file and sends a file to the client in response.
      • Client receives the server's file and sends a file to the server in response.
      • The last two steps repeat until some ending condition is reached.

      That sort of protocol tends to be self-synchronizing. Neither the server nor the client will send a file out of turn, so logic for handling out-of-turn files isn't necessary. If it weren't for the client taking initiative, nothing would happen at all.

      If you want to identify when one or the other side is desynchronized, then you can store a "sender" or "receiver" state in a variable, or in $_[HEAP]. The client begins in the "sender" state, and the server starts in "receiver" mode. If input arrives when a client or server is in "sender" mode, that's an error. Each side switches from sender to receiver upon successful transmission of a file. Each side switches from receiver to sender upon successful receipt of a file.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://816167]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (7)
As of 2024-04-23 17:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found