[erlang-questions] OTP compliant gen_tcp client

Vance Shipley vances@REDACTED
Sun Apr 19 21:11:40 CEST 2009

On Thu, Apr 16, 2009 at 10:07:04PM -0400, Vance Shipley wrote:
}  A lot has been written about gen_tcp server side implementation
}  strategies and I see that there are a lot of subtle issues 
}  to be considered to get it just right.

I call gen_tcp:connect/3 with {active, false} in a new process
and when it returns successfully I call gen_tcp:controlling_process/2
to make the main process the new owner of the Socket.  Then I send
the returned Socket to the main process.  On receipt the main gen_fsm
process calls inet:setopts/2 to set the Socket to {active, once} and
transitions to the connected state.

I was worried that I would have to then look to see if I'd received 
anything from the Socket, for example {tcp_closed, Socket}, and
forward it to the main process however I now understand that 
gen_tcp:controlling_process/2 does this for you.  Nice.

The only worry I have left is whether I should send the Socket to the
main gen_fsm process before or after calling gen_tcp:controlling_process/2.
If I send it first and then gen_tcp:controlling_process/2 then forwards
a received {tcp_closed, Socket} it would be the proper order.  On the
other hand if the main process calls inet:setopts/2 before the child
process finishes with gen_tcp:controlling_process/2 we have a problem.

Hmmm ....


More information about the erlang-questions mailing list