gen_tcp:send/2 but not gen_tcp:send/3 (3rd arg = timeout)

Scott Lystig Fritchie fritchie@REDACTED
Thu Feb 6 02:03:12 CET 2003


gen_tcp:recv() comes in two flavors: gen_tcp:recv/2 (no timeout) and
gen_tcp:recv/3 (third argument = timeout).  But there doesn't appear
to be a version of gen_tcp:send() that includes a timeout.

Looking into prim_inet.erl, there is no ability to specify a timeout.
:-(  I was hoping for an undocumented feature.

I've got a TCP proxy application (mentioned a few weeks ago on this
list, FWIW) that uses the follow simple-minded loop to do the proxying
work:

%% Note that, at the moment, we're a bit stupid by exiting whenever
%% either socket is closed or has an error.  At least in the case of
%% closure, a nice proxy would continue forwarding data in the other
%% direction.  However, that would mean we would have to carry more
%% state in State, horror!  :-)

proxy_loop(closed, closed, State) ->
    exit(byebye);
proxy_loop(CSock, SSock, State) ->
    receive
        {tcp, CSock, Data} ->
            gen_tcp:send(SSock, Data),
            proxy_loop(CSock, SSock, State);
        {tcp, SSock, Data} ->
            gen_tcp:send(CSock, Data),
            proxy_loop(CSock, SSock, State);
        {tcp_closed, Sock} ->
            exit(byebye);
        {tcp_error, Sock} ->
            error_logger:format("~s:proxy_loop: socket ~w ERROR\n", [?MODULE, So
ck]),
            exit(byebye);
        Msg ->
            error_logger:format("~s:proxy_loop: got ~w\n", [?MODULE, Msg]),
            proxy_loop(CSock, SSock, State)
    after State#state.timeout ->
            error_logger:format("~s:proxy_loop: TIMEOUT after ~w\n", [?MODULE, S
tate]),
            exit(byebye)
    end.

However, if someone had some process connected to the proxy, and if
that person were to press Control-z to suspend that process, then when
the proxy reads data from the other side and tries to send it to the
suspended process:

	A. The gen_tcp:send/2 call blocks temporarily, but eventually
	   proxy_loop() will be able to time out and exit.

	B. The gen_tcp:send/2 call blocks forever.

	C. None of the above.

Unfortunately, the answer is B.  In my proxy case, I have a scarce
resource allocated in conjunction with the proxy process; that
resource will only be deallocated when the proxy process dies.

Are there any plans to add a timeout value to gen_tcp:send()?

-Scott



More information about the erlang-questions mailing list