S
sujay.tukai
I am trying to establish a socket connection between one server and
multiple clients....
I have written codes for the server and the client side. But the
problems i face are
(1) i can connect one server to one client only, but i need multiple
clients.
(2) whenevr my client fails and i restart my client then i also have to
restart my server side prog too..
This is not applicable vice versa....
plz help i want to get some details like loads of my 5 client machines
using this program...
client side :
#!/usr/bin/perl -w
use strict;
use IO::Socket;
my ($host, $port, $kidpid, $handle, $handle1, $line, $msg);
$host = "192.168.0.119";
$port = "3579";
# create a udp connection to the specified host and port
$handle = IO::Socket::INET->new(Proto => "udp",
PeerAddr => $host,
PeerPort => $port)
or die "can't connect to port $port on $host: $!";
$handle1 = IO::Socket::INET->new(Proto => "udp",
LocalPort => $port)
or die "can't connect to port $port on $host: $!";
$handle->autoflush(1);
$handle1->autoflush(1);
print STDERR "[Connected to $host:$port]\n";
while (1) {
if($handle1->recv($line, 128)) {
if($line =~ m|send|) {
print "sending\n";
$line=`w`;
my ($ave)=$line =~ m/load average:\s(.*?),/s;
$handle->send($ave);
}
}
}
server side :
#!/usr/bin/perl -w
use strict;
use IO::Socket;
my ($host, $port, $kidpid, $handle, $handle1, $line, $msg);
unless (@ARGV == 2) { die "usage: $0 host port" }
($host, $port) = @ARGV;
# create a udp connection to the specified host and port
$handle = IO::Socket::INET->new(Proto => "udp",
PeerAddr => $host,
PeerPort => $port)
or die "can't connect to port $port on $host: $!";
$handle1 = IO::Socket::INET->new(Proto => "udp",
LocalPort => $port)
or die "can't connect to port $port on $host: $!";
$handle->autoflush(1);
$handle1->autoflush(1);
print STDERR "[Connected to $host:$port]\n";
while (1) {
sleep(5);
print "send\n";
$handle->send("send");
if($handle1->recv($line, 128)) {
print "$line\n";
}
}
multiple clients....
I have written codes for the server and the client side. But the
problems i face are
(1) i can connect one server to one client only, but i need multiple
clients.
(2) whenevr my client fails and i restart my client then i also have to
restart my server side prog too..
This is not applicable vice versa....
plz help i want to get some details like loads of my 5 client machines
using this program...
client side :
#!/usr/bin/perl -w
use strict;
use IO::Socket;
my ($host, $port, $kidpid, $handle, $handle1, $line, $msg);
$host = "192.168.0.119";
$port = "3579";
# create a udp connection to the specified host and port
$handle = IO::Socket::INET->new(Proto => "udp",
PeerAddr => $host,
PeerPort => $port)
or die "can't connect to port $port on $host: $!";
$handle1 = IO::Socket::INET->new(Proto => "udp",
LocalPort => $port)
or die "can't connect to port $port on $host: $!";
$handle->autoflush(1);
$handle1->autoflush(1);
print STDERR "[Connected to $host:$port]\n";
while (1) {
if($handle1->recv($line, 128)) {
if($line =~ m|send|) {
print "sending\n";
$line=`w`;
my ($ave)=$line =~ m/load average:\s(.*?),/s;
$handle->send($ave);
}
}
}
server side :
#!/usr/bin/perl -w
use strict;
use IO::Socket;
my ($host, $port, $kidpid, $handle, $handle1, $line, $msg);
unless (@ARGV == 2) { die "usage: $0 host port" }
($host, $port) = @ARGV;
# create a udp connection to the specified host and port
$handle = IO::Socket::INET->new(Proto => "udp",
PeerAddr => $host,
PeerPort => $port)
or die "can't connect to port $port on $host: $!";
$handle1 = IO::Socket::INET->new(Proto => "udp",
LocalPort => $port)
or die "can't connect to port $port on $host: $!";
$handle->autoflush(1);
$handle1->autoflush(1);
print STDERR "[Connected to $host:$port]\n";
while (1) {
sleep(5);
print "send\n";
$handle->send("send");
if($handle1->recv($line, 128)) {
print "$line\n";
}
}