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);

In reply to Chat room with socket programming question by Perl_New_Monk

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.