[Socket Programming]: Need info for Client / Server scenario
From: Suresh Pasupula (suresh.pasupula_at_wipro.com)
Date: 05/31/04
- Previous message: Andrew Gaffney: "Re: [Socket Programming]: Need info for Client / Server scenario"
- Next in thread: Andrew Gaffney: "Re: [Socket Programming]: Need info for Client / Server scenario"
- Reply: Andrew Gaffney: "Re: [Socket Programming]: Need info for Client / Server scenario"
- Reply: Lrmk: "Re: [Socket Programming]: Need info for Client / Server scenario"
- Reply: James Edward Gray II: "Re: [Socket Programming]: Need info for Client / Server scenario"
- Maybe reply: Suresh Pasupula: "RE: [Socket Programming]: Need info for Client / Server scenario"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Previous message: Andrew Gaffney: "Re: [Socket Programming]: Need info for Client / Server scenario"
- Next in thread: Andrew Gaffney: "Re: [Socket Programming]: Need info for Client / Server scenario"
- Reply: Andrew Gaffney: "Re: [Socket Programming]: Need info for Client / Server scenario"
- Reply: Lrmk: "Re: [Socket Programming]: Need info for Client / Server scenario"
- Reply: James Edward Gray II: "Re: [Socket Programming]: Need info for Client / Server scenario"
- Maybe reply: Suresh Pasupula: "RE: [Socket Programming]: Need info for Client / Server scenario"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|