This is a pritty involved field for me.

In perl FORKING will create a copy of memory of the existing thread so Data
sharing within the program is not posible
All the veriables are local to the thread.

To share data the stratagy I used is as follows.

The master thread or the data producer updates a database while all the
other threads are fetching data from that database.

Delays of responding client is one problem that I faced when using Forks in
Multy threaded servers in Windows Platform so far I have not been able to
fix that problem.

But you can use a different approach to give information to clients.
Why don't you use a web server as a server and write a CGI program to give
information to your child clients, the CGI will query the DB and give the
data to clients.

or The Master server can update a file (Text) insted of updating the
database, which can be directly accessable through the web server.
So each time the clients need data it download the file from the web server.

Even the master server can be written as a CGI program in a web server so
the master client simply post the data to its URL.

I think it is always a good Idear to avoid writing your own servers specialy
when there is a option.

________________________________________
Rakhitha Karunarathne
Web Master
www.Ad-Man.tk - Free Unlimited Banner Rotators
________________________________________



----- Original Message ----- 
From: <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Monday, May 31, 2004 10:06 AM
Subject: [Socket Programming]: Need info for Client / Server scenario


Hi,



I need some good links for understanding socket programming (Client /
Server programming).

I have written some scripts for multiple clients and a server (Forking
Server).

But I am facing some problems - the server data has to be shared among
all the child processes - does this happen when we fork - what should I
do for this to happen.



Requirement is:

"Normal clients" keep polling the server for some info.

Server will get some info from a "master client" till then the server
responds back with null info to the "Normal clients".

The server data that my master client is updating should get reflected
in all the child processes (of forked server) - this is not happening.
What should I do to achieve this?

Also I want my server to respond back immediately to all the clients
getting connected. This is not happening.



Kindly help me in solving the problem.



Following are the code snippets for clients and server:

Server  Code Snippet:

my $MySocket = new IO::Socket::INET(

                   LocalHost => $hostname,

                   LocalPort => 7890,

                   Proto     => 'tcp',

                   Listen    => SOMAXCONN,

                   Reuse     => 1);



$MySocket or die "Error: no socket :$!";



STDOUT->autoflush(1);



print "Server Program Started\n";

print "Waiting ...\n";



while ($client_sock = $MySocket->accept())

{

  # execute a fork, if this is

  # the parent, its work is done,

  # go straight to continue

  next if $kid = fork;

  die "fork: $!\n" unless defined $kid;

  # child now...

  # close the server - not needed

  close $MySocket;

  STDOUT->autoflush(1);

  print "\nACCEPTED: New Client\n";

  while (defined($buf = <$client_sock>))

  {

    chomp ($buf);

    print "Recievied: $buf\n";

    if ($buf =~ / NORMAL_CLIENT/i)

    {

          ... # return NULL if $val is NULL

              # else return proper value

$buf = $val;

    }

    elsif ($buf =~ /MASTER_CLIENT/)

    {

          ... # read the info

   # update the variable $val if not NULL

$val = <$client_sock>; (SERVER DATA)

chomp $val;

if ($val ne '')

{

$buf = "SUCCESS";

          }

          else

          {

                   $buf = "FAILURE";

}

    }

    #send msg to client

    print $client_sock "$buf\n";



}

exit;

}

continue

{

  close $client_sock;

}



Normal Client Code Snippet:

while ($TRUE)

{

  $MySocket = new IO::Socket::INET(

                  PeerAddr => $ServerName,

                  PeerPort => $port,

                  Proto    => 'tcp');

  if (!$MySocket)

  {

    print "Failed to Connect to Server: $ServerName\n";

    print "Trying to connect again after 10 seconds\n";

    sleep (10);

    next;

  }

  else

  {

    last;

  }

}



print "Connected to \n";

print "Processing ...\n";



while ($TRUE)

{

    print $MySocket "NORMAL_CLIENT_"."$host\n";

    $Message = <$MySocket>;

    chomp $Message;



    if ($Message eq ''})

    {

      print "NULL Msg from Server. Polling after (time in seconds):
",$TIME_DELAY * $ONE_MINUTE,"\n";

      sleep ($TIME_DELAY * $ONE_MINUTE );

    }

    else

    {

        print "Msg from Server: $Message\n";

    }

}#end of while

close $MySocket;





Master Client Code Snippet:

while ($TRUE)

{

  $MySocket = new IO::Socket::INET(

                  PeerAddr => $ServerName,

                  PeerPort => $port,

                  Proto    => 'tcp');

  if (!$MySocket)

  {

    print "Failed to Connect to Server: $ServerName\n";

    print "Trying to connect again after 10 seconds\n";

    sleep (10);

    next;

  }

  else

  {

    last;

  }

}



print "Connected to \n";

print "Processing ...\n";



print $MySocket "MASTER_CLIENT_"."$host\n";

print $MySocket "$Information\n"; #this msg has to be sent to Server
where all other Normal clients are waiting for this info

$Message = <$MySocket>;

chomp $Message;

print "Msg from Server: $Message\n";



close $MySocket;



Thanks in advance

Suresh



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to