question about "active" option (regarding gen_tcp)

Patrik Winroth <>
Wed Nov 7 19:51:52 CET 2001


On Wed, 7 Nov 2001, Rick Pettit wrote:

>
> I have a concurrent TCP server which spawns a new process every time
> gen_tcp:accept() returns.  I call gen_tcp:listen() and set active to
> true, such that tcp packets arrive as messages in my mailbox.
>
> I had hoped to call gen_tcp:controlling_process() immediately after my
> call to accept, so that any TCP packets for that connection will be sent
> to the spawned processes mailbox.
>
> I fear that there is a race condition between the call to accept and the
> call to controlling_process (which means the parent may receive TCP
> packets in its mailbox instead of the child).
>
> Any suggestions?

I think ( :-) ) one of the ways to do this is:

1) Use the option {active, false} when creating the listen socket.

2) When you get an accept socket, spawn a process to handle it, set the
spawned process to controlling process for the socket,
send a message to the spawned process to get it to use

inet:setopts(Accept_socket, [{active,true}])

3) messages are delivered to your process.

Note: If you expect lots of incoming data to your server you should
consider using passive sockets instead, see

http://www.erlang.org/doc/r8b/lib/kernel-2.7/doc/html/index.html

Regards,

/Patrik.

-- 
Patrik Winroth                         <>
Vindaloo AB                            mbl: 0709-727364




More information about the erlang-questions mailing list