[erlang-questions] gen_tcp and processes receiving

Stephan Maka stephan@REDACTED
Thu Oct 4 21:10:41 CEST 2007


Hi

I am having trouble receiving from TCP sockets in processes that did not
open it. The processes are not distributed over multiple nodes but `erl'
is invoked with `-smp'.

The attached module simply received nothing. When I receive in the
process that opened the socket it works. In the program where I need
this functionality the gen_tcp:recv result is {error, ebadf}. I did some
googling on it, but all people with the same problem have gotten no
answer to it.

I'm unsure about the use of gen_tcp:controlling_process when I simply
use a passive socket. Active sockets are no option here, I have strong
traffic shaping requirements.

Thanks for any tips in advance,
Stephan
-------------- next part --------------
-module(tcp_ebadf).

-export([start/0]).


start() ->
    io:format("Connecting...~n"),
    {ok, Socket} = gen_tcp:connect("www.erlang.org", 80, [{active, false}]),

    io:format("Sending...~n"),
    gen_tcp:send(Socket, "GET / HTTP/1.1\r\n"
		 "Host: www.erlang.org\r\n"
		 "Connection: close\r\n"
		 "\r\n"),

    Pid = spawn_link(fun() ->
			     receive
				 go ->
				     ok
			     end,
			     receiver(self(), Socket)
		     end),
    ok = gen_tcp:controlling_process(Socket, Pid),
    Pid ! go,

    io:format("Waiting...~n"),
    receive
	{byte, _B} ->
	    io:format("Ok, I received a byte~n")
    after 10000 ->
	    exit(timeout)
    end,
    
    gen_tcp:close(Socket).


receiver(Listener, Socket) ->
    io:format("Receiving...~n"),
    {ok, [B]} = gen_tcp:recv(Socket, 1),
    Listener ! {byte, B}.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20071004/a9c27cc9/attachment.bin>


More information about the erlang-questions mailing list