Hi, I'm new to perl (allthough Im pretty good with php and delphi) and have been making some small programs. I just started a program and allready am stuck. I am trying to make a server/client app. All that th server does is accept connections, and on reading data from a client, sends it to every other one. The client should connect to the server, receive data from it, and send data to it, a very very simple chat application in short. I am having problems with the server, to manage the different connections, I am using the select() function, as described here:
http://www.perlfect.com/articles/select.shtml
My code allmost exactly the same as that, and it still deosnt work, for a start I would like it to print the data received from the clien on the screen, what is wrong here (it never prints the data to the screen)?:
#!/usr/bin/perl
#test script
use IO::Socket;
use IO::Select;
if(@ARGV < 1)
{
printf("Usage:\ntest.pl <port to run on>\n");
exit();
}
my $sock = IO::Socket::INET->new(
Proto=>"tcp",
LocalHost=>"Localhost",
Listen=>16,
Reuse=>1,
LocalPort=>$ARGV[0]
) or die("Could not create socket!\n")
+;
my $readSet = new IO::Select();
$readSet->add($sock);
while(1)
{
my $rhSet = IO::Select->select($readSet, undef, undef, 0);
foreach $rh(@rhSet)
{
if($rh == $sock)
{
$newSocket=$rh->accept();
$readSet->add($newSocket);
} else
{
$buf=<$rh>;
if($buf)
{
printf "$buf";
} else
{
$readSet->remove($rh);
close($rh);
}
}
}
}
I have allready anticipated another problem I will have. As you see, it accepts any new sockets, but what if, I want to send data to all open sockets the server has received? I see no where (array, etc) where the sockets are saved. Any help?
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.