Using Sockets connecting multiple clients


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";
}
}
 
Ad

Advertisements

J

Jim Gibson

I am trying to establish a socket connection between one server and
multiple clients....

You are using the UDP protocol, which does not create connections.
Socket objects of this protocol may be used to send or receive
individual datagrams between one host and another. See 'perldoc
perlipc' and search for 'UDP: Message Passing' for more info.

You can use the select statement to query whether or not datagrams have
been received from a number of remote hosts, asynchronously.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top