TCP Server+perl



I'm trying to do socket communication between two machines Linux and
MVS.
On Linux side, the code is programmed in Perl and on the MVS side in
Rexx.

The Perl code I have is designed as a TCP Server which should process
several Clients at the same time.

I've used Forks for this. I close the Parent Server and make the child/
Client processes orphans because I want to to spawn of a new child/
Client each time a new connection is established at the Socket.

But here is what happens. Lets say in the beginning 4 connections are
established by the socket, 4 child processes are spawned off, When
each child completes processing it closes/ends. In the meanwhile 4
more connections are already established at the socket and are waiting
to be spawned off (and ideally each new child should be spawned off
immediately, irrespective if the previous children have finished
processing or not.).
However in my current scenario, if one of the child process takes more
time to complete, the 4 new established connections still wait, for
the slower child to finish. It's only when all 4 children spawned off
in the first go are completed, that the 4 new waiting processes are
spawned off.

I want each child to spawn off as soon as the connection is made.

I believe that perhaps my parent is not being closed properly. Here is
the piece of code.
Any suggestions are welcome.

-Thanks,
Gauri

# Main loop control variable
$time_to_die = 0;

# Set up signal handler, which just sets the global to exit the main
loop
$SIG{INT} = $SIG{TERM} = $SIG{HUP} = \&SIG_HANDLER;

# Build the socket: standard Perl boilerplate
socket(SERVER, PF_INET, SOCK_STREAM, getprotobyname('tcp'));
setsockopt(SERVER, SOL_SOCKET, SO_REUSEADDR, 1);
$my_addr = sockaddr_in($PORT,INADDR_ANY);
bind(SERVER,$my_addr) or do {
$s="Couldn't bind to port $PORT";
mylog($s,"crit");
die "$s : $!\n";
};
listen(SERVER, SOMAXCONN) or do {
$s="Couldn't listen on port $PORT";
mylog($s,"crit");
die("$s : $!\n");
};

# Main listen/fork loop: straight-ahead forking server. You exit the
main
# loop by sending a signal to the program, which then sets the global
variable
# $time_to_die in the signal handler, and then the next time through
the
# loop, the program exits and shuts down.

until($time_to_die) {
$client = accept(CLIENT, SERVER);
select CLIENT; # By default, print to socket
$| = 1; # Turn on autoflush so prints happen
# immediately and there's no
buffering
next unless ($client);
($port,$packed_ip) = sockaddr_in($client);
$dq = inet_ntoa($packed_ip);
mylog("Connection from $dq : $port");
# Fork and exec main server loop
$pid = fork();
die "fork: $!" unless defined $pid;
if ($pid) {
# Parent
} else {
mylog("Child starting with connection from $dq : $port");
close(SERVER); # child doesn't need it
mainloop(); # Go do the actual work
mylog("Child exiting");
exit 0;
}
}

continue {
close(CLIENT); # Parent doesn't need it
}

mylog("End of main event loop reached (signal, most likely): Exiting
now.");

close (SERVER);
exit 0;

# End of mainline code

.



Relevant Pages

  • Re: trying to understand fork and wait
    ... old habits based on learning to script in REXX on the ... > the child reads it. ... situation for me (drop through to bottom/go back to top of loop). ... just to keep a hold of the exit code. ...
    (comp.lang.perl.misc)
  • Re: newbie question about fork/pipe/stdin/stdout
    ... If readreturns 0 (indicating EOF), exit the loop. ... If you have data to write to the child and pipeStdinis writeable, ...
    (comp.unix.programmer)
  • Re: mysql vs. fork
    ... what's the point in forcing an exit while we are exiting? ... only the at_exit handlers defined in the child process are ... That's bad because the server thinks he has one peer only. ... And if you use the connection's close method chances are that the server shuts down the socket and both clients suffer. ...
    (comp.lang.ruby)
  • Re: Only one usage of each socket address (protocol/network address/po
    ... If I do something like the below, the intllisense tells me that ... Where you call it now would be fine, if you had any way to exit the loop. ... I'd use the asynchronous API so that I didn't have a thread sitting blocked at a call to an Accept method, and thus I could stop and close the socket wherever I like without worrying about some thread that I needed to coordinate with. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: File descriptor suddenly goes bad
    ... I had thought that there was some requirement to use _exit() in the child? ... If you open a file descriptor then fork the child inherits the open FD. ... Now closing it in one process is not a problem but some other socket ...
    (comp.os.linux.development.apps)