Using Sockets connecting multiple clients

Discussion in 'Perl Misc' started by sujay.tukai@gmail.com, Nov 20, 2006.

  1. Guest

    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";
    }
    }
    , Nov 20, 2006
    #1
    1. Advertising

  2. Jim Gibson Guest

    In article <>,
    <""> wrote:

    > 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.
    Jim Gibson, Nov 20, 2006
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tiro
    Replies:
    0
    Views:
    658
  2. Replies:
    2
    Views:
    1,028
  3. Francesco
    Replies:
    1
    Views:
    508
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=
    Dec 27, 2006
  4. John P

    sockets and connecting to other programs

    John P, Jan 30, 2005, in forum: C Programming
    Replies:
    3
    Views:
    259
    Dave Thompson
    Feb 7, 2005
  5. Todd M. Taylor

    Multiple Roles with Multiple Clients

    Todd M. Taylor, Mar 25, 2005, in forum: ASP .Net Security
    Replies:
    0
    Views:
    143
    Todd M. Taylor
    Mar 25, 2005
Loading...

Share This Page