Perl_New_Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello everyone! I am brand spanking new here, and somewhat new to perl. I started to learn perl about 2 months ago.
I am trying to create a simple chat room using perl with socket programming. I have 2 main files. One is the server, and the other is the client. I wanted to add a feature and that is when the client tries to connect, I want the server to prompt server admin to accept or reject the client. If Accepted, chat fine! If not, disconnect this client and keep serving others.
Thanks,
server.pl
#!/usr/bin/perl -w use strict; use Socket; # For constants like AF_INET and SOCK_STREAM use IO::Select; $| = 1; my($proto, $port , $sock , $s , @ready , $so , $addrinfo , $client , $ +inp); $proto = getprotobyname('tcp'); #get the tcp protocol socket($sock, AF_INET, SOCK_STREAM, $proto) or die "could not create s +ocket : $!"; $port = $ARGV[0]; bind($sock , sockaddr_in($port, INADDR_ANY)) or die "bind failed : $! +"; listen($sock , 10); print "Server is now listening ...\n"; $s = IO::Select->new(); $s->add($sock); while(1) { @ready = $s->can_read(0); foreach $so(@ready) { if($so == $sock) { my($client); $addrinfo = accept($client , $sock); my($port, $iaddr) = sockaddr_in($addrinfo); my $name = gethostbyaddr($iaddr, AF_INET); print "Connection accepted from $name : $port \n"; send($client , "Hello client how are you\n" , 0); $s->add($client); } else { chop($inp = <$so>); chop($inp); print "Received -- $inp \n"; send($so , "OK : $inp\n" , 0); } } } close($sock); exit(0);
client.pl
#!/usr/bin/perl -w use Socket; # For constants like AF_INET and SOCK_STREAM $proto = getprotobyname('tcp'); #get the tcp protocol my($sock); socket($sock, AF_INET, SOCK_STREAM, $proto) or die "could not create s +ocket : $!"; my $remote = $ARGV[0]; my $port = $ARGV[1]; $iaddr = inet_aton($remote) or die "Unable to resolve hostname : $remo +te"; $paddr = sockaddr_in($port, $iaddr); #socket address structure connect($sock , $paddr) or die "connect failed : $!"; print "Connected to $remote on port $port\n"; print "Waiting for approval from $remote ...\n"; my $approved = <$sock>; if ($approved eq 'yes') { print "Welcome, you were approved by $remote\n"; while (<STDIN>) { my $input = <STDIN>; send($sock , "I am a client!" , 0) or die "sendo failed : $!"; if ($input eq 'end_chat') { close($sock); exit(0); } } send($sock , "I am a client!" , 0) or die "sendo failed : $!"; while ($line = <$sock>) { print $line; } } else { print "Sorry, you were rejected by $remote\n"; close($sock); exit(0); } exit(0);

Replies are listed 'Best First'.
Re: Chat room with socket programming question
by kcott (Archbishop) on Nov 30, 2016 at 23:11 UTC
Re: Chat room with socket programming question
by VinsWorldcom (Prior) on Nov 30, 2016 at 20:19 UTC

    When I run your code, I get a bunch of errors when the client tries to connect. I'm on Windows 7 x64 with Strawberry 5.22.1 64-bit.

    It may be just adding the prompt statements in the "if($so == $sock)" block of your server code before accept() call.

    This doesn't directly answer your question, but maybe some useful advice? Your code looks a lot like it was ported from C using Socket calls directly. Perl has IO::Socket in core and IO::Socket::IP in newer versions which even allows IPv6 and IPv4 - if you're interested in that.

      Yes! I was trying to write the feature I wanted but failed. I tested your code.. works perfect, but like you said, it does not have the feature of accepting and rejecting clients.
      I will work more on it and update this post if I find a way to do it.
      Thank you..
        The code you have posted works, but it can only serve one client. I tried connecting a second client but it did not work. I have Windows 7 64 bits and perl 5.22
Re: Chat room with socket programming question
by tybalt89 (Monsignor) on Dec 01, 2016 at 15:56 UTC

    How does the server talk to the server admin. Console? Or as a client of the server? On a separate port? Or are usernames and passwords required?