Electric Type

Multimedia

About Us

News

Help

How to Write a Chat Server

Page 5 — Now Broadcasting

All you need to do to turn "echo" into a chat server is send messages to everyone, not just yourself. We can use $select, which returns all the sockets we gave to $select using new() or add(), to find out who "everyone" is. So we make these changes:

            $socket->recv($line,80);
            if($line eq "") {
                print $socket->fileno . ": disconnected\n";
                $select->remove($socket);
                $socket->close;
            };
            my $socket;

            # broadcast to everyone.  Close connections where send() fails.
            
            for $socket ($select->handles) {
                next if($socket==$listen);
                $socket->send($line) or do {
                    print $socket->fileno . ": disconnected\n";         
                    $select->remove($socket);
                    $socket->close;
                };
            }
            

Here's the full source for chat.

next page»


Tutorials Home  

CSS Reference  

Regular Expressions  

Image Filtering  

Adding Site Search  

Image Maps  

Browser Detection  

Fundamentals of XSSI  

FTP Tutorial  

HTML 4.0  

User Blogs

Screen Shots

Latest Updates

Contact Us

Valid HTML 4.01!
Valid CSS!

Breadcrumb

© ElectricType
Maintained by My-Hosts.com
Site map | Copyright | Disclaimer
Privacy policy | Acceptable Use Policy
Legal information.