Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Aug 20th, 2005, 3:45 PM   #1
nick
Newbie
 
Join Date: Aug 2005
Posts: 1
Rep Power: 0 nick is on a distinguished road
New to perl: Network programming question

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?
nick is offline   Reply With Quote
Old Aug 22nd, 2005, 4:44 AM   #2
prolog
Programmer
 
Join Date: Jul 2005
Location: Germany
Posts: 69
Rep Power: 4 prolog is on a distinguished road
Hi,

you'll have to use the operations IO:ocket provides for reading and writing. Check the man-page for that. Secondly to get an array of all sockets that are ready to write to call IO:ocket::can_write. This will return an array of all handles that are writable.

my @writers=$sel->can_write;
foreach my $cur (@writers){
# .....do sth
}

If it's just for learning do it that way. If not consider using POE.

So far ...
__________________
-= C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do succeed, you will blow away your whole leg. =- Bjarne Stroustrup
prolog is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:56 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC