...I'll try to make it short. Forgive my newbism. Since I made chat server allowing multiple clients, I wanted to make a GUI for the client. The GUI is in Tk and the client is expected to put the address and their name in the fields and then launch a connection from a button... this loads a subroutine which I want to use to take the strings from the fields, create a socket, recieve messages constantly and insert it to the beginning of the Listbox. Unfortuneately, when I do this it just freezes because it's only focusing on this routine and no messages are currently sent. here's the code:
use 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); } }
I ask of a solution to have the receiving of messages run in the background

In reply to Tk and socket question by un be-knoweth

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.