I ask of a solution to have the receiving of messages run in the backgrounduse Tk; use IO::Socket::INET; $er1 = "Connection was not yet made..."; $er2 = "No current sockets to close..."; $main = MainWindow->new(); $main->title('Conors Chat Client'); $main->minsize(50, 5); $main->configure(-background=>'black'); $top = $main->Frame(-background => 'black')->pack(-side=>'left', -fill +=>'x'); $options = $main->Frame(-background => 'black')->pack(-side=>'right', +pady=>8, padx=>8); $mess = $top->Frame(-background => 'black')->pack(-side=>'left', pady= +>8, padx => 8); $box = $mess->Listbox(-relief => 'sunken', -width => 50, -height => 5, -setgrid => 'yes', -background=>'Dark + Red'); $scroll = $mess->Scrollbar(-command => ['yview', $box]); $box->configure(-yscrollcommand => ['set', $scroll]); $box->pack(-side => 'left', -fill => 'both', -expand => 'yes'); $scroll->pack(-side => 'left', -fill => 'y'); @hello = ("Welcome to Chat Client V1.0"); foreach (@hello){ $box->insert('end', $_); } $msg1 = $mess->Entry(-width=>8, -background =>'Dark Red')->pack(-side= +>'bottom'); $mess->Button(-text=> 'Send', -background=>'Dark Red', -command=> sub{ +sender($msg1)})->pack(-side=>'bottom'); $options->Label(-text => 'Server Host:', -background => 'Dark Red')->p +ack; $serverip = $options->Entry(-width=>8, -background =>'Dark red')->pack +; $options->Label(-text => 'Name:', -background => 'Dark Red')->pack; $name = $options->Entry(-width=>8, -background => 'Dark red')->pack; $options->Button(-text => 'Start Connection', -background => 'Dark red', -command => sub{connection($serverip, $name)})->pack; $options->Button(-text => 'Stop Connection', -background => 'Dark red' +, -command => sub{endconnect})->pack; $options->Button(-text => 'Exit', -background => 'dark red', -command +=> sub{exit})->pack(-side=> 'bottom'); MainLoop; sub connection { ($serverip, $name) = @_; $servadd = $serverip->get; $name1 = $name->get; @serv = split(/:/, $servadd); print "Server: $serv[0] and port: $serv[1]"; <b> $sock = new IO::Socket::INET->new(PeerAddr=>$serv[0], PeerPort= +>$serv[1],Proto=>'tcp') or die "Can't Connect!!!"; while($sock){ $sock->recv($msg, 100); if($msg ne ''){ $box->insert('active', $msg); } }</b> } sub endconnect { if(defined ($sock)){ close ($sock); } else { $box->insert('active', $er2); } } sub sender { $mess1 = $msg1->get; if(defined ($sock) and msg1 ne ''){ $sock->send("$name1: $mess1"); $box->insert('active', $name1.':'.$mess1); } else { $box->insert('active', $er1); } }
In reply to Tk and socket question by un be-knoweth
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |