Perl_New_Monk has asked for the wisdom of the Perl Monks concerning the following question:
client.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);
#!/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 | |
by Perl_New_Monk (Initiate) on Nov 30, 2016 at 21:21 UTC | |
by Perl_New_Monk (Initiate) on Nov 30, 2016 at 21:24 UTC | |
by VinsWorldcom (Prior) on Nov 30, 2016 at 21:30 UTC | |
|
Re: Chat room with socket programming question
by tybalt89 (Monsignor) on Dec 01, 2016 at 15:56 UTC |