I'm trying to create a simple client/server using IO::Socket, and well, under Linux everything runs just fine. However, my boss, being a... boss, has decided he wants me to write a Win32 client for him (ugh, what a nightmare this is going to be). Anyhow, my first problem with getting my client to work is that fork() doesn't seem to work properly on Win32 with ActivePerl 5.6.1.631 (Latest Build as far as I can tell. It's really bothering me, as everywhere I look everyone says 'fork() works fine in 5.6... etc'. Basically, through sockets, I'm doing a simple 'Client send server data. Server look at data, send response back to client', and they do this until one or the other decides it's time to close the socket. Here's my code for the client which works just great under linux. Any help in rewriting this thing would be greatly greatly appreciated:
#!/usr/bin/perl
use IO::Socket;
# Make our connections
my $ns1 = new IO::Socket::INET (PeerAddr => shell,
PeerPort => 1200,
Proto => 'tcp'
) or die "Couldn't create socket: $!\n"
+;
# Now let's fork the client so we can read and write
my $pid = fork();
$fork_errlvl = 1 unless defined $pid;
# If we weren't able to fork...
die "Error forking client: Reason: $!\n" if $fork_errlvl != 0;
if ($pid) {
write_sock();
waitpid($pid, 0);
} else {
read_sock();
}
sub write_sock {
foreach my $key (@ARGV) {
print $ns1 "$key\n";
sleep(1);
}
close($ns1);
}
sub read_sock {
while (my $line = <$ns1>) {
chomp($line);
print "$line\n";
if ($line eq 'CAIO') {
close($ns1);
}
}
}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.