Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

POE TCP client problem

by baldeep8 (Initiate)
on Jan 07, 2010 at 15:46 UTC ( [id://816131] : perlquestion . print w/replies, xml ) Need Help??

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

We tried POE::Component::Client and POE::Component::Server modules to communicate between client and server programs on different ports on the same machine. I have not used the Client Input and Server Input components on the Server and Client respectively. (Meaning I have used them but left them blank). Instead I have obtained the socket handle from ARG0 in ClientPreConnect in Server and Connected in Client. The goal is to be able to send multiple XML files back and forth between the client and server on multiple ports. Using the obtained socket handle I am using sysread and syswrite functions to read and write data in the socket. I find if I use a single port then data gets sent and received properly. But when I use 2 ports on server and client then no data gets sent or received. I only get the message that one of the ports is connected. I am not sure if I am not properly using the POE::Kernel->run. But I am not clear about why this is happening. I am a newbie to POE. Please help.

The code below is the server:
#!/usr/bin/perl #use strict; # use both POE server and the filter needed use POE qw(Component::Server::TCP Filter::Reference); use CUPPS::Cupps; my $Dest = Value("DestinationXMLLocation"); my $Src = Value("SourceXMLLocation"); POE::Component::Server::TCP->new( Alias => "sum_server", Port => 11211, ClientFilter => "POE::Filter::Reference", ClientPreConnect => sub { sleep(1); $socket = $_[ARG0]; sysread($socket, $buf, 1000); print "Result = $buf\n"; open(FH,"$Src\\interfaceLevelsAvailableResponse-OK.xml"); $buf = join("", <FH>); close(FH); print "\n"; syswrite($socket, $buf, 1000); print "\n"; $buffer = $buf; while ($buffer eq $buf) { sysread($socket, $buf, 1000); } print "Result = $buf\n"; open(FH,"$Src\\interfaceLevelResponse-OK.xml"); $buf = join("", <FH>); close(FH); syswrite($socket, $buf, 1000); }, ClientInput => sub { }, ); POE::Component::Server::TCP->new( Alias => "sum_server", Port => 11212, ClientFilter => "POE::Filter::Reference", ClientPreConnect => sub { sleep(1); $socketd = $_[ARG0]; sysread($socketd, $bufd, 1000); print "Result = $bufd\n"; open(FHD,"$Src\\authenticateResponse-OK.xml"); $bufd = join("", <FHD>); close(FHD); print "\n"; syswrite($socketd, $bufd, 1000); print "\n"; $bufferd = $bufd; while ($bufferd eq $bufd) { sysread($socketd, $bufd, 1000); } print "Result = $bufd\n"; open(FHD,"$Src\\byeResponse-OK.xml"); $bufd = join("", <FHD>); close(FHD); syswrite($socketd, $bufd, 1000); }, ClientInput => sub { }, ); $poe_kernel->run(); exit 0;
The code below is the Client:
#!/usr/bin/perl -w #use strict; use POE; use POE::Component::Client::TCP; use POE::Filter::Reference; #use POE::Session::MultiDispatch; use CUPPS::Cupps; my $host = "localhost"; # The host to test. my $port = 11211; my $Dest = Value("DestinationXMLLocation"); my $Src = Value("SourceXMLLocation"); POE::Component::Client::TCP->new( RemoteAddress => $host, RemotePort => $port, #SessionType => "POE::Session::MultiDispatch", Filter => "POE::Filter::Reference", PreConnect => sub { my ($socket, $peer_addr, $peer_port) = @_[ARG0, ARG1, ARG2]; print "connected to $host:$port ...\n"; open(FH,"$Src\\interfaceLevelsAvailableRequest.xml"); my $buf = join("", <FH>); close(FH); syswrite($socket, $buf, 1000); print "\n"; $buffer = $buf; while ($buffer eq $buf) { sysread($socket, $buf, 1000); } print "Result = $buf\n"; open(FH,"$Src\\interfaceLevelRequest.xml"); $buf = join("", <FH>); close(FH); print "\n"; syswrite($socket, $buf, 1000); sleep(1); print "\n"; $buffer = $buf; while ($buffer eq $buf) { sysread($socket, $buf, 1000); } $socket->autoflush(1); print "Result = $buf\n"; }, Connected => sub { }, ConnectError => sub { print "could not connect to $host:$port ...\n"; }, ServerInput => sub { }, ); my $port1 = 11212; POE::Component::Client::TCP->new( RemoteAddress => $host, RemotePort => $port1, #SessionType => "POE::Session::MultiDispatch", Filter => "POE::Filter::Reference", PreConnect => sub { my ($socketd, $peer_addrd, $peer_portd) = @_[ARG0, ARG1, ARG2]; print "connected to $host:$port1 ...\n"; open(FHD,"$Src\\authenticateRequest.xml"); my $bufd = join("", <FHD>); close(FHD); syswrite($socketd, $bufd, 1000); print "\n"; $bufferd = $bufd; while ($bufferd eq $bufd) { sysread($socketd, $bufd, 1000); } print "Result = $bufd\n"; open(FHD,"$Src\\byeRequest.xml"); $bufd = join("", <FHD>); close(FHD); print "\n"; syswrite($socketd, $bufd, 1000); sleep(1); print "\n"; $bufferd = $bufd; while ($bufferd eq $bufd) { sysread($socketd, $bufd, 1000); } print "Result = $bufd\n"; }, Connected => sub { }, ConnectError => sub { print "could not connect to $host:$port ...\n"; }, ServerInput => sub { }, ); POE::Kernel->run(); exit 0;

Replies are listed 'Best First'.
Re: POE TCP client problem
by rcaputo (Chaplain) on Jan 07, 2010 at 19:16 UTC

    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, ""); }
      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.