[erlang-questions] gen_tcp:recv and hairloss

Christian chsu79@REDACTED
Sat Nov 8 11:35:14 CET 2008


> Basically I can't read from the socket unless I come back up out of the
> Fsm and let the same loop get the next line.  What is so
> special about the gen_server process that can *read* from the socket and
> the fsm process that cant?!?

The process that accepts the connection is the owner of that socket, also
called the controlling process. Only the owner can recv.  You can
transfer ownership
using gen_tcp:controlling_process/2 (from the current owner).

What I typically do is to follow the design where in your case the
gen_fsm would start out
as an ordinary process spawned by proc_lib, then using
gen_fsm:enter_loop when a connection
has been accepted.

The server process will then start these acceptor processes, wait for
them to report that a aconnection
has been accepted, and thus start a new acceptor process.

This always feels a bit clunky to set up, but the pay off in cleaner
design makes it worth it. Your server
process is never blocked. While an acceptor process is looping around
in gen_tcp:accept until a connection
is accepted, the server process is free to accept calls and casts for
commands sent to or from already
accepted connections.



More information about the erlang-questions mailing list