in reply to Re: Newbies TCP Networking
in thread Newbies TCP Networking

Like I said.....By a newbie :)

I didn't use IO::Select because I couldn't find any useful examples for it. Since I really don't know what I'm doing I had to go by what I could find examples for.

Thanks for the $socket->close(). Again with little docs and based on examples I found I didn't even know I could do it that way.

I indent my code as I program, It makes for easy readibility (I use a 4 space tab)...Sorry that it screwed up so much on the page.

My "paranoid" client is a scaled down uncleaned version. I had 3 conditions but removed one and I also feel that including an extra (even if impossible) else statement is good for debugging.

I've got lots to learn about IPC. I don't think I understand it too well. I was using some server code found on the site and when it forked it seemed to lose the connection or something. Someone suggested putting a sleep(5) line in and that seemed to fix it, but I wasn't satified with that kind of a fix. I wanted to see if I could get the processes to talk to each other so they could confirm having started before closing any connections. I think I accomplished that, but then again I don't really know much about IPC.

(That comment about IO:Handle and the thousands of lines is from a search: perlipc example....I just didn't take it out.)

:) Thanks for the comments

Replies are listed 'Best First'.
Re^3: Newbies TCP Networking
by tadman (Prior) on Jul 10, 2001 at 23:38 UTC
    If you're really feeling "paraniod", you have to be very careful not to fall in the "defensive programming" mode which can be very damaging. With defensive programming, your function which should do something, but receives bad input, does nothing. You just created a ticking time-bomb, because sooner or later some other function is going to set it off, and you'll have no idea where the problem came from.

    Consider:
    sub OpenSomething { my ($param) = @_; # What kind of an idiot would forget the foo parameter! return unless (defined $param->{foo}); my $thing = new Object ($param->{foo}, $param->{zoo}); $thing->open($param->{foozoo}); return $thing; } sub CloseSomething { my ($thing) = @_; $thing->close(); }
    Now, in your program you will call OpenSomething(), which might actually return nothing. You forget to check if you got anything back, so later on when you call CloseSomething() you get an error. The error, it would seem, originates in CloseSomething().

    Of course, you could just get more defensive:
    sub CloseSomething { my ($thing) = @_; # Maybe they forgot to pass the parameter? return unless (defined $thing); $thing->close(); }
    Now what you have done, effectively, is swept all these errors under the carpet. They don't manifest, and they don't cause "errors" in the visible sense, but there may be problems with the way your program runs that doesn't make any sense.

    It is probably better to die and get it over with:
    sub OpenSomething { my ($param) = @_; # What kind of an idiot would forget the foo parameter! die "No 'foo' param passed to OpenSomething()" unless (defined $ +param->{foo}); my $thing = new Object ($param->{foo}, $param->{zoo}); $thing->open($param->{foozoo}); return $thing; } sub CloseSomething { my ($thing) = @_; die "Cannot CloseSomething() with undefined parameter" unless (d +efined $thing); $thing->close(); }
    Using die in this capacity is similar to the ASSERT() macro used in C programming which will completely halt the program if something is seriously out of line.

    If the train comes off the tracks, you'd best stop it.
      I can see your point.

      I've always tried to throw something kind of error in my functions if things don't go right. I think a die or an error code is good.

      I probably forget often enough, I haven't really used subs/functions since I started in Perl. I need to get it down. I guess the funny way of passing variables into them kind of turned me off. And since I don't do anything too complex (most of what I do is fairly procedural) I haven't had a real need for it.

      In the future, maybe :)
Re: Re: Newbies TCP Networking
by otijim (Acolyte) on Jul 10, 2001 at 23:09 UTC
    OOps Forgot to login :) Otijim