Re: [[email protected]: question on IPC::LDT implementation with Event.pm]
[email protected] (Jochen Stenzel) Wed, 18 Aug 2004 23:12:59 +0200
| Newsgroups | perl.loop |
|---|---|
| Message-ID | <[email protected]> |
Hello,
JNP> In your Event tutorial, you mention on using non-blocking socket
JNP> using IPC::LDT perl module. I try to use but encounter errors on
JNP> the receive. here's my snipprt code. I hope u can point out my
JNP> errors.
I don't know the errors that occurred, but here is a slightly reworked
version of your code that I could run successfully:
- snip --
# precondition to make IPC::LDT::new(traceMode=>1) work
BEGIN {$IPC::LDT::Trace=1;}
# load modules
use Event;
use IPC::LDT;
use IO::Socket;
# make socket
my $socket=IO::Socket::INET->new(Listen=>5, LocalPort=>8001, Reuse=>1);
die "[Fatal] Cannot build initial socket.\n" unless $socket;
DEBUG && print ("start smpp service\n");
# install an initial watcher
Event->io(
fd => $socket,
cb => sub {
# make "channel" number
my $channel=++$channels;
# accept client
my $client=$socket->accept;
warn "[Error] Cannot connect to new client.\n" and return unless $client;
$client->autoflush;
# install a communicator
Event->io(
fd => $client,
cb => sub {
# report
warn "[Server] Talking on channel $channel.\n";
my $ldt = new IPC::LDT(handle=>$_[0]->w->fd, traceMode=>1);
my $msg = $ldt->receive() or die $ldt->{'msg'};
warn "[Channel 1] $msg\n";
if ( $msg =~ /quit$/i){
warn "[Server] Closing channel $channel.\n";
$_[0]->w->cancel;
}
}
);
# welcome
$client->print("Welcome, this is Channel $channel.\n");
# report
warn "[Server] Opened new channel $channel.\n";
},
);
# start loop
Event::loop;
- snip --
To talk with this server, one needs to use IPC::LDT as well (or to
emulate its protocol). For example, I used the Perl debugger, like
so:
- snip --
> use IO::Socket::INET
> use IPC::LDT
> $s1=new IO::Socket::INET(PeerAddr=>'localhost:8001')
> $ldt1=new IPC::LDT(handle=>$s1)
> $ldt1->send("Hi, there!")
> $s2=new IO::Socket::INET(PeerAddr=>'localhost:8001')
> $ldt2=new IPC::LDT(handle=>$s2)
> $ldt2->send("Nice to talk to you!")
> $ldt1->send('quit')
> $ldt2->send('quit')
- snip --
I suppose the usage of IPC::LDT in the *client* was the important
point here, was it? Could you please try if it works for you this way?
Jochen
signature.asc
(application/pgp-signature, 205 B)
-----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.4 (MingW32) - GPGrelay v0.950 iD8DBQBBI8ZfGiYRWTCU/vQRAk1pAKCW06LGHUhxcxWjAbZmnUCBZn3cLQCg2ed9 xcCUCfC2QuR3ftC3PPk/W6w= =ZcwQ -----END PGP SIGNATURE-----