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:
and a server:#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;
I am requesting for help on implementing the ideas I mentioned above.#!/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";
In reply to Re^2: Building a chat server and client
by deadpickle
in thread Building a chat server and client
by deadpickle
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |