Thanks for the replies, they are very helpful! But I'm still lost.
The server should listen for incoming messages. When it receives messages it should then broadcast them to all the connected users. Also, the server is in charge of the connected users. This entails checking for duplicate usernames and informing the connected users of new and leaving users. The thing is that I don't know where to start in this whole diboggle. I'm not familiar with sockets and writing server scripts.

For the client it should connect to the server and share its user name. The users connection should be rejected if there is a similar username or should be forced to have a different name. with that taken care of, it should work just like a chat client does. you type and then the message is sent to the server where it is broadcast to all servers even the owner.

I have been trying to build the scripts from scratch and so far I have a client:

#server #!/usr/bin/perl use warnings; use strict; use IO::Socket; my $SERVER = "Deadpickle-hobo"; my $SEND_PORT = 5152; my $line; my $send_socket = IO::Socket::INET->new( PeerAddr => $SERVER, PeerPort => $SEND_PORT, Proto => 'tcp', Reuse => 1 ) or die "socket: $@"; #ask for the user name and send it to the server #in order to verifiy that its unique print "Username:"; my $user = <>; $send_socket->send($user); $send_socket->recv($line, 80); print $line; $send_socket->recv($line, 80); print $line;
and a server:
#!/usr/bin/perl -w use strict; use IO::Socket; use IO::Select; #variables my $LISTEN_PORT = 5152; my $user; my @users; my $fh; my @ready; #open a new socket my $listening_socket = IO::Socket::INET->new( LocalPort => $LISTEN_PORT, Proto => 'tcp', Listen => 1, Reuse => 1 ) or die "socket: $@"; my $select = new IO::Select($listening_socket); #wait for a client to connect to the server print "Awaiting TCP messages on port $LISTEN_PORT\n" if $listening_soc +ket; while(@ready = $select->can_read) { foreach $fh (@ready) { if($fh == $listening_socket) { #Create a new socket and get the user ID my $new = $listening_socket->accept; $new->recv($user, 80); #Check the newly connected user for copies if (my $new_user = grep($user, @users)) { } else { $select->add($new); push(@users, $user); $new->send("Hello\n"); $new->send("@users"); } } else { # Process socket # Maybe we have finished with the socket $select->remove($fh); $fh->close; } } } print "END\n";
I am requesting for help on implementing the ideas I mentioned above.

In reply to Re^2: Building a chat server and client by deadpickle
in thread Building a chat server and client by deadpickle

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.