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

Hi,

I have been pulling my hair out for this one. I am trying to write a game, which would communicate to other players via sockets. However, there can be anywhere from 2 to 6 players in the game, so, waiting for new players to join is causing me some headache. Since I didn't want to force the players into joining a game within a certain timeout period, I make a blocking socket without a timeout value. Thus, the host must tell the game when to stop awaiting more players. Here's what I came up with (see below).

The host forks: the parent sits awaiting a click of the "Cancel" button to stop the child process, the child sits awaiting the socket connections, getting the names of the players. The child receives the names just fine, but since the $name var is not shared, i need to pass it back to the parent. AND THIS DOES NOT SEEM TO WORK!

Please help me out, or,if you see a better way of doing this, let me know. Thanks in advance.P.S. to run the "host", simply run the program, to run as a joining player, run program with any option.

use Tk; use IO::Socket; use strict; use English; require Tk::DialogBox; if (defined $ARGV[0]){ my $sock = new IO::Socket::INET ( PeerAddr => 'localhost', PeerPort => '7071', Proto => 'tcp', ); die "Could not create socket: $!\n" unless $sock; print $sock "name=Player1!"; close($sock); exit; } my $mw = MainWindow->new; &waiting; MainLoop; sub waiting{ my $sock; my $pid; my $name; pipe READOUT, WRITEOUT or die "can't pipe"; $pid = fork(); if ($pid){ my $db = $mw->DialogBox( -title => "Waiting for users", -buttons => [ "Cancel" ] , -command => sub {kill(30,$pid);}, ); $db->add( "Label", -text => " \n\n")->pack(); $db->Show(); close(WRITEOUT); my $in = <READOUT>; print "parent received $in\n"; close(READOUT); } else { close(READOUT); open(STDOUT, ">&WRITEOUT"); sub SeeYa { print "\n";close(STDOUT);close(WRITEOUT);} $SIG{'USR1'} = SeeYa; for (my $i=1; $i<6; $i++){ $sock = new IO::Socket::INET ( LocalHost => 'localhost', LocalPort => 7071, Proto => 'tcp', Listen => 1, Reuse => 1, ); die "Could not create socket: $!\n" unless $sock; my $new_sock = $sock->accept(); my $input; while(defined(my $in = <$new_sock>)) { $input = $in; } ($_,$name) = split(/=/,$input); close($sock); print "$name\n"; } } }

Replies are listed 'Best First'.
Re: Fork and pipe
by fokat (Deacon) on Aug 30, 2002 at 17:41 UTC
    The correct way to do this, is to avoid the fork() altogether, as you don't really need it.

    Probably your game will incorporate an event loop at its core. This would be something along the lines of:

    while (1) {
    
      # do each round of your game here
    
      if ($the_player_wants_to_exit) { &my_exit_method(); }
    
    }
    
    In fact, since your game involves network players, your loop most likely looks like:

    while (1) {
    
      &get_a_message_from_a_player();
    
      # Do the rest of your game round processing here
    
      if ($the_player_wants_to_exit) { &my_exit_method(); }
    
    }
    
    What I would do, is to embed the processing of new players inside the same event loop. This would be something along the lines of:
    while (1) {
    
      &get_and_process_new_player();
      &get_a_message_from_a_player();
    
      # Do the rest of your game round processing here
    
      if ($the_player_wants_to_exit) { &my_exit_method(); }
    
    }
    
    IMO, this will make your life easier. Take a look at IO::Socket and IO::Select, as they are of great help for what you need here.

    Regards.

Re: Fork and pipe
by Zaxo (Archbishop) on Aug 30, 2002 at 16:01 UTC

    You're prints from the child are buffered. Set $| = 1; in the child, after you reopen STDOUT.

    I see a couple of other problems. The parent appears to only read one name, and the child does not exit 0; when done.

    After Compline,
    Zaxo

      Setting $| = 1 does not seem to do anything. I had tried it out before, and opted to leave it out now. As for the parent reading only one name, well, I need to be able to read the first one before moving onto "greater challenges" :-).
Re: Fork and pipe
by kschwab (Vicar) on Aug 30, 2002 at 16:32 UTC
    You may want to have a look at the built-in Tk mechanisms to handle I/O within the Gui event loop.

    This would allow the incoming sockets to be dealt with without forking. See:

    Update The docs do mention some unsavory behavior on Win32...bah. Sorry. I have little Win32 experience or I wouldn't have suggested it.
      The following code still seems unresponsive (can't click on button, and label is not refreshed). This must be a windows thing.

      use Tk; use IO::Socket; use strict; my $sock; my $port = 7076; my $time = 60; my $mw = MainWindow->new; my $button = $mw->Button(-text=>"click", -command=>sub{exit;})->place(-x=>0,-y=>0); my $l = $mw->Label(-text=>"$time")->place(-x=>0,-y=>50); $mw->update; $sock = new IO::Socket::INET ( LocalHost => 'localhost', LocalPort => $port, Proto => 'tcp', Listen => 1, Reuse => 1, #Timeout => 0.1, ); die "Could not create socket: $!\n" unless $sock; $mw->fileevent($sock,'readable',[\&readit]); $mw->repeat(100,sub{$l->configure(-text=>$time--);$mw->update;}); MainLoop; sub readit{ my $new_sock = $sock->accept(); my $in = <$new_sock>; print "received $in\n"; }
      Looking at the documentation, Tk::fileevent would be excellent in my case. However, I cannot get it to work in a noneblocking manner. The execution seems to stop while processing the $mainwindow->fileevent($socket,'readable',[\&some_sub]); line. Have you seen an example with a socket read fileevent callback? (that works on Windows?). Thanks
        I must retract this statement. The blocking was happening NOT a the fileevent line, but the line just above that, $socket->accept(); (duhhh). I need a break :-)