TCP proxy using gen_server tutorial (Part 1)

Jay Nelson <>
Mon Mar 17 08:45:26 CET 2003

This will be a 3 part tutorial.  Part 1 is complete, Part 2 will
follow shortly and Part 3 will take a few weeks.  It covers
using gen_server to deliver a functionality similar to Joe's
nano-webserver middleman.  Please let me know of any
problems you have with the code or the tutorial.

The tutorial is at

The code is at
and echo.erl (a braindead test driver accessible via telnet
-- start via tcp_proxy(echo) and then 'telnet localhost 8000').

There are a few problems with the code as it sits:

1) If I kill the accept process, it is not caught in handle_info
and it takes down the entire gen_server.  I tried to trap_exit
in tcp_proxy:start_link, but it just occurred to me I need to
trap_exit where the accept process is spawned.   [I thought
it was automatically trapped by gen_server and reported in
handle_info.  As it stands the 'EXIT' clause of handle_info
is never called.  'DOWN' works just peachy.]

2) If I start the server like:  {ok, P2} = tcp_proxy:start_link(echo),
and then erroneously call tcp_proxy:report_clients(P1) or any
other function, the gen_server gets a badmatch, but takes down
the tcp_proxy server with it.  Why isn't P1 a different beast,
especially if it is a non-existent process, and why should it
affect the P2 process?  Are all gen_server-based processes linked to
the gen_server and will all come down if you make a typo in the

3) When the server goes down, the existing clients remain
connected.  I thought when a Listen socket was closed, all
Accept sockets derived from it would automatically be closed.
As it is any previously connected clients will still get serviced
after the server is down (I'm not sure if this is good or bad).


More information about the erlang-questions mailing list