Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

IO::Socket fork win32 bug ? or i su......

Status
Not open for further replies.

NEVERSLEEP

Programmer
Apr 14, 2002
667
CA
# hi , im having problems with fork on :
# nt , perl 5.6.1 built for MSWin32-x86-multi-thread
# the fork is in a simple sender/receiver 'client'
# the bug :
# when the 'client' send a message it freeze
# server dosen't receive until abort of client ????
# below are the bogus parts....any tip welcome ...
# *i dont understand fork at all (kill also) ...
#
# client fork
#...
&error("Could not connect to server ..." , "probably down") unless $sock;
&error("System could not fork ...", chomp $!) unless defined(my $thafork = fork());
if ($thafork) {
while (defined ($content = <$sock>)) {
print &quot;$content\n&quot;;
}
kill(&quot;TERM&quot;, $thafork);
} else {
while (defined ($content = <STDIN>)) {
chomp $content;
print $sock $content;
}
}
}
#...
# server loop
while (my $client = $server->accept()) {
$client->autoflush(1);
print $client &quot;Welcome !\nInput>&quot;;
print &quot;[Connect from &quot;. $client->peerhost . &quot;]\n&quot;;
while (<$client>) {print &quot;[From &quot; . $client->peerhost . &quot;] &quot; . $_ . &quot;\n&quot;;}
close $client;
}

# thanks ---------------------------------------
wmail.jpg


someone knowledge ends where
someone else knowledge starts
 
I'm not too sure how your code is supposed to work, but I see one problem and have one improvement. When using fork, you need to make the child exit or else it will continue and execute the parent's code. Also adding some error checking on the fork would be a good idea.
Code:
$thafork = fork());
if ($thafork) {
    # parent
    while (defined ($content = <$sock>)) {
        print &quot;$content\n&quot;;
    }
    kill(&quot;TERM&quot;, $thafork);
} elsif (defined $thatfork) {
    # child
    while (defined ($content = <STDIN>)) {
        chomp $content;
        print $sock $content;
    } 
    exit;
} else {
    print &quot;Can't fork: $! \n&quot;;
}


 
hi raider i tried the 'exit' method here and it didn't work
same thing as before ...

# here the full code .. please help me
# client
use strict;
use IO::Socket;

my $content;
unless ($ARGV[0]) {&fordummy;}
$ARGV[0] =~ tr/a-zA-Z0-9[.]/ /cs;
$ARGV[0] =~ s/\s//g;
if ($ARGV[0] ne &quot;&quot;) {&loop($ARGV[0]);} else {&fordummy;}

sub loop {
print &quot;Connecting to Server ...\n&quot;;
my $sock = new IO::Socket::INET (
PeerAddr => $_[0],
PeerPort => '7070',
Proto => 'tcp'
);
&error(&quot;Could not connect to server ...&quot; , &quot;probably down&quot;) unless $sock;
&error(&quot;System could not fork ...&quot;, chomp $!) unless defined(my $thafork = fork());
if ($thafork) {
while (defined ($content = <$sock>)) {
print &quot;$content\n&quot;;
}
kill(&quot;TERM&quot;, $thafork);
} else {
while (defined ($content = <STDIN>)) {
chomp $content;
print $sock $content;
}
}
}

sub fordummy {print &quot;Usage : pch_dv serverip\nInfos : your ip is &quot; . &returnip;exit;}
sub error {print &quot;$_[0]\n$_[1]&quot;; exit;}
sub returnip {
@_ = `ipconfig`;
foreach (@_) {
if (/IP Address/) {
@_ = split(/: /, $_);
$_[1] =~ tr/\n/ /;
$_[1] =~ s/\s//g;
return $_[1];
last;
}
}
}
# server
use strict;
use IO::Socket;

my $server = IO::Socket::INET->new(
Proto => 'tcp',
LocalPort => '7070',
Listen => SOMAXCONN,
Reuse => 1
);

die &quot;can't setup server&quot; unless $server;
print &quot;[Server status : running accepting clients]\n&quot;;

while (my $client = $server->accept()) {
$client->autoflush(1);
print $client &quot;Welcome !\nInput>&quot;;
print &quot;[Connect from &quot;. $client->peerhost . &quot;]\n&quot;;
while (<$client>) { print &quot;[Msg From &quot; . $client->peerhost . &quot;] &quot; . $_ . &quot;\n&quot;;}
close $client;
}

print &quot;[Server status : shut down]&quot;;
exit;

# done code
# thanks ---------------------------------------
wmail.jpg


someone knowledge ends where
someone else knowledge starts
 
actually this line
print $client &quot;Welcome !\nInput>&quot;;
is bogus it just print
Welcome !\n\n\n
???? (bonus \n ?)
not the Input>
?
am i suposed to use a /xxx/ code here ? (in thesocket ?)
thanks ---------------------------------------
wmail.jpg


someone knowledge ends where
someone else knowledge starts
 
Hey there,
I have never had much luck making fork work on Windows boxes. I did a little poking around and found out that fork() does not really work very well with that OS, I run Linux at home. The reason is that windows is a multi-threaded operating system and *nix's are multi-process. Both achieve the same end result but get there differently. The long and short of all this ho-hum is that you may save yourself some grief, and create a more robust product, if you try to use threads with perl in windows.
 
ya i know scoon fork does not worth s*** on windows
i flushed my fork idea here i turned to a tk solution ...
---------------------------------------
wmail.jpg


someone knowledge ends where
someone else knowledge starts
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top