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:The code below is the Client:#!/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;
#!/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 | |
by baldeep8 (Initiate) on Jan 08, 2010 at 07:21 UTC | |
by rcaputo (Chaplain) on Jan 11, 2010 at 06:43 UTC |