|
I want to develop a chat application using perl.
My code is given below.
If I tried this code in two separate machines,it won't work.Anyone help me.
use strict;
use warnings;
use threads;
use IO::Socket::INET;
my $port = 7745;
my $thread = threads->create("server_thread");
my $thread1 = threads->create("client_thread");
sub server_thread {
my %params = (
LocalHost => '10.10.97.20',
LocalPort => $port,
Proto => 'tcp',
Listen => 1,
Reuse => 1,
ReuseAddr => 1,
);
my $so = new IO::Socket::INET(%params);
die "Server Socket Error: $!\n" unless $so;
print STDERR "[Server Socket Connected]\n";
my $client = $so->accept();
$client->autoflush(1);
while (1) {
my $txt = <$client>;
last unless defined($txt);
chomp $txt;
last if ($txt eq 'quit');
print "server : $txt\n";
}
print "[Server finished]\n";
}
sub client_thread {
sleep 1; # Give the server a head start
my %params = (
'PeerAddr' => '10.10.97.20',
'PeerPort' => $port,
'Proto' => 'tcp',
'ReuseAddr' => 1,
);
my $so = new IO::Socket::INET(%params);
$so->autoflush(1);
die "Client Socket Error: $!\n" unless $so;
print STDERR "[Client Socket Connected]\n";
while (my $msg = <STDIN>) {
chomp $msg;
last if ($msg eq 'quit');
print $so "$msg\n";
}
print "[Client finished]\n";
}
$thread->join();
$thread1->join();
|
|
|
|
|
|
|
// |