Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Help with socket reading and returning

by basicdez (Pilgrim)
on Mar 14, 2002 at 14:16 UTC ( #151676=perlquestion: print w/replies, xml ) Need Help??

basicdez has asked for the wisdom of the Perl Monks concerning the following question:

This is the code I am currently using to read from a socket (that piece is working) and then I want to return a message to the same socket (I cannot seem to get this to work). If any of you would have the time to look this over and give me some advice, that would be awesome. I am also getting an error from the code that is telling me that states
"Use of uninitialized value at /develop/release/cmsidev/dev/cr/cds/comms/x2p/pd2x line 130, < +FILE1> chunk 1 (#2) (W) An undefined value was used as if it were already defined. It + was interpreted as a "" or a 0, but maybe it was a mistake. To suppre +ss this warning assign an initial value to your variables."
#!/usr/bin/perl -w BEGIN { $CHANGEVAR = $ENV{CRROOTWSDIR}; if ($CHANGEVAR =~ /^([-\/\w.]+)$/) { $MAINPATH = $1; } else { die "Invalid path, please check setenv"; } $LIB = "$MAINPATH/cr/cds/comms/x2p"; } use lib "$LIB"; my $runpath = "$ENV{CRROOTWSDIR}/cr/cds/comms/x2p"; use diagnostics; use strict; use sigtrap; use IO::Select; use IO::Socket::INET; use X2PCONF; $SIG{CHLD} = 'IGNORE'; ### Kill zombie processes immediately. $SIG{PIPE} = 'IGNORE'; ### Ignore RST (reset flag) if there's an + error writing to socket. $| = 1; ### Enable Autoflushing of I/O. { my $sock = new IO::Socket::INET ( LocalAddr => $X2PCONF::cdsd_host, LocalPort => $X2PCONF::pd2x_port, Proto => 'tcp', Listen => 5, Reuse => 1, ); die "Could not create socket\n" unless $sock; my $sock_new; my $buffer; CONNECTION: while ($sock_new = $sock->accept()) { if ((my $chldpid = fork ()) != 0) { close ($sock_new); next CONNECTION; } my $buf = ' '; while (defined ($buf = <$sock_new>)) { if ($buf eq "\"ZZZ\"\n") { $buffer .= "\"EOS\""; last; } else { $buffer .= $buf; } } last; } print " $buffer \n"; open(DATA, ">$runpath/pd2x.dat") or die ("error opening pd2x.dat $ +!\n"); print DATA $buffer; print "$runpath/pd2x.dat \n"; system ("perl $runpath/packets2a.pl"); my $message; my $fname_response = "$runpath/response.dat"; my $check_value = ' '; open FILE1, "< $fname_response" or die "Cannot open datfile: ", $!; while (<FILE1>) { $check_value = $_; if ($check_value ne "END") { $message = ($message . $check_value); } else { next; } } close (FILE1); print $sock ($message); print "Response packet $message \n"; close ($sock); } exit (0);
If any of you have any advice for this frustrated programmer please let me know.

dez

Edit by dws to add <readmore>

Replies are listed 'Best First'.
•Re: Help with socket reading and returning
by merlyn (Sage) on Mar 14, 2002 at 14:24 UTC
    If any of you have any advice for this frustrated programmer please let me know.
    Yes. While I didn't read any of your code except to see its length, I have one fundamental rule regarding net connections:
    Don't invent a protocol if an existing one will do.
    In particular, have you looked to see if you can use HTTP as your transport layer? There are many advantages to that. If you need clean RPC, you can layer SOAP on top of HTTP, otherwise you can simply use the HTTP content as your payload, or encode everything in the virtual URL. Setting up an HTTP server is as simple as a few lines of code, as illustrated in HTTP::Daemon's documentation.

    One cool advantage is that HTTP can tunnel through HTTP proxies, but your random protocol cannot. Another is that you can set up an "administrative interface" on your mini-server and talk to it with a browser.

    -- Randal L. Schwartz, Perl hacker

Re: Help with socket reading and returning
by Caillte (Friar) on Mar 14, 2002 at 14:36 UTC

    You do not initialise $message before the first time you call:

    $message = ($message . $check_value);

    Correct this by using:

    my $message = '';

    This page is intentionally left justified.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://151676]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2022-01-18 15:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:












    Results (53 votes). Check out past polls.

    Notices?