This is one of my first ventures into the world of sockets....
I'm trying to through together a program that will allow multiple users to exchange information. Currently
I'm picturing a server to which clients connect, but I'm not wedded to that design yet.
I've been reading through Chapter 12 of the panther book, and I'm looking at the following code:
use IO::Socket;
use IO::Select;
$main_socket = new IO::Socket::INET (LocalHost => 'localhost',
LocalPort => 1200,
Listen => 5,
Proto => tcp,
Reuse => 1);
die "Socket not created: $!\n" unless ($main_socket);
$readable_handles = new IO::Select();
$readable_handles->add($main_socket);
while(1){
#select() blocks until a socket is ready to be read
($new_readable) = IO::Select->select($readable_handles,undef,undef
+,0);
#We now have at least one readable handle.
foreach $sock (@$new_readable){
if ($sock == $main_socket){
$new_sock = $sock->accept();
#new connection, add to list.
#May not be readable yet.
$readable_handles->add($new_sock);
} else {
#check to see if socket closed
$buf = <$sock>;
if($buf) {
#Do stuff with $buf
} else {
#socket was closed
$readable_handles->remove($sock);
close($sock);
}
}
}
}
My questions are:
- The next section in the book says that the filehandle blocks, but I don't understand this. Can someone restate it for me?
- While I'm expecting to trust my clients, I can see problems if one gets stuck in an infinite loop and sends me continuous data...how would I stop that? Particularly if I'm trying to read all the info the client sends before acting on it.
- This code has an infinite loop, which is obviously incompatible with a GUI. I'd like to make this a package that can be used by different UIs. I have an idea
of how to do this (put the loop into subroutine, make it for a limited time rather than infinite, ditto with the select, and call it via a timed event in the calling program), but any advice on pitfalls
to avoid would be appreciated.
- Because the clients will also be modules that can be GUI based, they'll probably be polling the server to check for status changes. Assuming the clients all sent a did-anything-change query once a second, would the above code allow everyone to get talked to, or would one client have priority over the others?
- This all sounds like its creating some significant network traffic...is this normal? I may just be nervous since I'm used to the mostly non-interactive web.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.