yes, there is.

the natural way is you install a Tk::fileevent handler for the listening
socket. Your "readable"-handler is called when an incomming connection
can be accepted on the socket. But if you are using Tk on a Windows box
then fileevent won't probably work. In that case it's the best to poll
the socket. Something like that:

# the handler
use IO::Select;
use vars qw/*LISTENING_SOCKET/;
sub acceptor {
  my $sel=IO::Select->new( *LISTENING_SOCKET );
  while( $sel->can_read(0) ) {
    # accept connection and handle request
  }
}

$widget->repeat( $poll_interval, \&acceptor ); # on MSWin32
$widget->fileevent( *LISTENING_SOCKET, 'readable', \&acceptor ); # on Unix

If you want to know more on the subject I can recommend you the book 
Mastering Perl/Tk. There are also some fine points where a program can 
block in the accept call even if a previous select call reported the file
descriptor as readable. On that topic I can recommend you the 1st volume of
UNIX Network Programming by Richard Stevens.

On Sun, 17 Feb 2002 16:35:21 -0500
"John V. Pataki" <[EMAIL PROTECTED]> wrote:

> 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" }
> 
> my $port = shift || 2345;
> 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: $!";
> 
> logmsg "server started on port $port";
> 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";
> }
> -----------------------------------------
> 
> 
> 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);
> 
> ---------------------------------------------------------
> 
> 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
> 
> 
> 
> 
_______________________________________________
Perl-Win32-Users mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to