gen_tcp:send failing silently

fgonthier <>
Mon Feb 23 02:46:26 CET 2004


Things have evolved since last message.  I've managed to put the little bot
online.

> If you're willing to see one that I've been developing using OTP, tell
> me and I'll post the code somewhere (it's work in progress, I didn't want
> to reuse an old one I had written some years ago).

I wanted to somewhere impress the guy.  He hand-coded a channel service bot
for our channel in C.  The bot is around 5700 lines of C code and I bet to
him (and to me as an self-challenge) that I could make a feature-complete
clone of it in Erlang in less than 500 lines.

His bot has more feature than I expected so I'll be pretty when I'll have an
Erlang infobot clone.

> | connection_handle_data(Socket, [Data | R]) ->
> |     % Handles PING
> |     case regexp:match(Data, "PING :[0-9].*") of
>
> This is overkill, you should just compare the beginning to "PING " and
> answer with "PONG " and the rest of the string.

Yeah, that's what I thought too, but the most elegant things with pattern
matching is:

case Data of
	[80,73,78,71] -> ...

and that gets ugly when the string is longer.  I'm probably missing
something about pattern-matching.

> You do not need to "prepare" the string yourself. You can send a list
> using gen_tcp:send/2, it will be flattened for you. In your
> example, that
> would be something like:
>
>   ok = gen_tcp:send (Socket, ["PONG ", Ping_String, "\r\n"])

Well noted!

> Are you sure it gets never displayed? Isn't it caused by the
> missing ~n?

Okay, that's the part I solved.  If you look back at my original message,
you can see:

	    Ping_String = string:substring(Data, 6, 16),

But, string:substring doesn't exist!  It's what was crashing this process.

I have coded a custom supervisor process to manage the 3 process (I have yet
to pick up on OTP).  I used link() to link my workers to my supervisor,
expecting the supervisor would receive {'EXIT'...} when other processes
failed.  I was missing the process_flag(trap_exit, true) call.  Because all
my processes were linked, when the connection was failing, all my processes
died without any error...

but still, the process dies with a the 'normal' reason.  That's kinda hard
to handle...

I'm slightly bothered by this behavior.  An error like a mistyped function
like "substring" should for raise some kind of warning on the console.
Perhaps I'm too used to stricter language where such an error is treated as
a catastrophe: "Compiler error: OMG! THAT FUNCTION DOESN'T EXIST! RUN FOR
YOUR LIFE"...

Any advices here?  I'm a simple comp. sci. student that usually prefers Java
for homeworks.

Francois-Denis





More information about the erlang-questions mailing list