I am trying to write
an perl/Tk application that will allow user interaction while at the same time
monitoring a socket for other apps that want to talk to it.
I seem to be able to
do either or...
I can write a simple
server to listen to the socket as in the example below....
(examples from
O'reilly)
-----------------------------
use Socket;
use Carp;
sub logmsg { print "@_ at ", scalar localtime, "\n" }
use Carp;
sub logmsg { print "@_ at ", scalar localtime, "\n" }
my $port = shift ||
2345;
my $proto = getprotobyname('tcp');
my $proto = getprotobyname('tcp');
socket(Server,
PF_INET, SOCK_STREAM, $proto) or die "socket: $!";
setsockopt(Server, SOL_SOCKET, SO_REUSEADDR, pack("l", 1))
or die "setsockopt: $!";
bind(Server, sockaddr_in($port, INADDR_ANY)) or die "bind: $!";
listen(Server,SOMAXCONN) or die "listen: $!";
setsockopt(Server, SOL_SOCKET, SO_REUSEADDR, pack("l", 1))
or die "setsockopt: $!";
bind(Server, sockaddr_in($port, INADDR_ANY)) or die "bind: $!";
listen(Server,SOMAXCONN) or die "listen: $!";
logmsg "server
started on port $port";
my $paddr;
$SIG{CHLD} = \&REAPER;
my $paddr;
$SIG{CHLD} = \&REAPER;
for ( ; $paddr =
accept(Client,Server); close Client) {
my($port,$iaddr) = sockaddr_in($paddr);
my $name = gethostbyaddr($iaddr,AF_INET);
logmsg "connection from $name [",
inet_ntoa($iaddr), "] at port $port";
print Client "Hello there, $name, it's now ",
scalar localtime, "\n";
}
my($port,$iaddr) = sockaddr_in($paddr);
my $name = gethostbyaddr($iaddr,AF_INET);
logmsg "connection from $name [",
inet_ntoa($iaddr), "] at port $port";
print Client "Hello there, $name, it's now ",
scalar localtime, "\n";
}
-----------------------------------------
or even a simpler
one like this
----------------------------------------
use
IO::Socket;
$sock = new IO::Socket::INET (LocalHost => 'localhost',
LocalPort => 1200,
Proto => 'tcp',
Listen => 5,
Reuse => 1
);
die "Socket could not be created. Reason: $!" unless $sock;
while ($new_sock = $sock->accept()) {
while (defined ($buf = <$new_sock>)) {
print $buf;
}
}
close ($sock);
$sock = new IO::Socket::INET (LocalHost => 'localhost',
LocalPort => 1200,
Proto => 'tcp',
Listen => 5,
Reuse => 1
);
die "Socket could not be created. Reason: $!" unless $sock;
while ($new_sock = $sock->accept()) {
while (defined ($buf = <$new_sock>)) {
print $buf;
}
}
close ($sock);
---------------------------------------------------------
and I have written
several perk/Tk apps that do various items.
But once, I add the
socket listening code to the perl/Tk app... I am locked.
Is there a way to
have the same app listen to a socket and remain unlocked to do other
tasks?
It seems to me it
would involve somehow making the listen or accept commands operate in an eval
block or something.
But I haven't gotten
that to work yet.
Thanks for any
help,
John