[Socket Programming]: Need info for Client / Server scenario

From: Suresh Pasupula (suresh.pasupula_at_wipro.com)
Date: 05/31/04

  • Next message: Lrmk: "Re: [Socket Programming]: Need info for Client / Server scenario"
    Date: Mon, 31 May 2004 09:36:16 +0530
    To: <beginners@perl.org>
    
    

    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


  • Next message: Lrmk: "Re: [Socket Programming]: Need info for Client / Server scenario"

    Relevant Pages

    • RE: Users Cant Access Documents on Server
      ... Thanks for using the SBS newsgroup. ... As well as we know, if a workstation would not access network shares, then ... Leave the Default Gateway of the internal NIC blank of the server box. ... Clients That Require SMB Signing ...
      (microsoft.public.windows.server.sbs)
    • Re: Users Cant Access Documents on Server
      ... my computer to the network on the server. ... Connection Wizard none of the computers were listed. ... The Mac clients can not communicate with the server box. ... > Error Messages When You Open or Copy Network Files on Windows XP SP1 ...
      (microsoft.public.windows.server.sbs)
    • Re: [SLE] SMTP authentication
      ... So eventhough my local SMTP server dials up to the internet with a certain username and password, that same username and password would not be used as authentication between my local SMTP server and the ISP's one, should it be used as a relay? ... either defer all outgoing mails until you connect to the internet, then flush out all the mails in the queue. ... Your local server would use an external program like fetchmail to poll the mailserver of your ISP, download the mails and feed them to Postfix. ... The test does NOT say "All clients must be in mynetworks, ...
      (SuSE)
    • RE: VPN Clients Not Registering in AD DNS
      ... via VPN, the DNS records of the VPN clients are unable to be registered. ... Windows 2003 server? ... please let me know whether the clients get the IP ...
      (microsoft.public.windows.server.sbs)
    • Re: Users Cant Access Documents on Server
      ... > then add my computer to the network on the server. ... Did you not see the computers in the Server Management taskpad section? ... The Mac clients can not communicate with the server box. ... >> Error Messages When You Open or Copy Network Files on Windows XP SP1 ...
      (microsoft.public.windows.server.sbs)