my 2 cents... (This is a snippet of code from a Server using IO::Socket, all of the code is not there but this is the main server portion..... take it for what you like...
my ($acceptSock, $pid, $apprCode, $bufHdr, $buf, $length, $hdr, $req);
my ($p1, $rsp, $rspMsg, $rspHdr, $msg);
while (1)
{
printf("%sListening on port $args{p}\n", prHdr());
if (!($acceptSock = $listenSock->accept()))
{
print "bkpt 1: Accept failed: $!\n" if ($debug > 2);
next;
}
# Spawn off a child to handle this connection
$pid = fork();
die "Cannot fork: $!" unless defined($pid);
# PID is zero when we're the child
if($pid)
{
print "Parent continues\n" if ($debug > 2);
next;
}
printf("%sAccepted connection from: %s\n", prHdr(), $acceptSock->pee
+rhost());
srand(time);
$apprCode = sprintf("%06d", int(rand 99999)+1);
# Wait indefinitely for a message
while (1)
{
print "Error reading 2-byte header: $!\n", last
if (sysread($acceptSock, $bufHdr, 2) != 2);
printf("%sReceiving request\n", prHdr());
# We got the 2-byte (data length) header
$length = unpack("n", $bufHdr);
print "$$: Length from request header = $length\n"
if($debug > 2);
print "Error length ($length) is < e-Header size ($EHDR_SIZE)\n",
+last
if ($length < $EHDR_SIZE);
print "Error reading $length bytes of data: $!\n", last
if (sysread($acceptSock, $buf, $length-2) != $length-2);
if($debug > 1)
{
# Got the data, display it
&hexDump($bufHdr . $buf);
print "\n";
}
}
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.