[erlang-questions] how does an erlang Socket/Port look from inside?

Fred Hebert <>
Wed Mar 26 17:59:16 CET 2014

Hi, I have a solution.

As part of recon (which allows to use strings and whatnot as ports) to
diagnose issues, I have developed a term_to_port function that uses the
serialized binary form of a port and uses binary_to_term/1 to make it
into a real Erlang term:


    %% @doc Transforms a given term to a port
    -spec term_to_port(recon:port_term()) -> port().
    term_to_port(Port) when is_port(Port) -> Port;
    term_to_port(Name) when is_atom(Name) -> whereis(Name);
    term_to_port("#Port<0."++Id) ->
        N = list_to_integer(lists:sublist(Id, length(Id)-1)), % drop trailing '>'
    term_to_port(N) when is_integer(N) ->
        %% We rebuild the term from the int received:
        %% http://www.erlang.org/doc/apps/erts/erl_ext_dist.html#id86892
        Name = iolist_to_binary(atom_to_list(node())),
        NameLen = iolist_size(Name),
        Vsn = binary:last(term_to_binary(self())),
        Bin = <<131, % term encoding value
                102, % port tag
                100, % atom ext tag, used for node name
                N:4/unit:8, % actual counter value
                Vsn:8>>, % version

I'm guessing this could be useful in your case here, if gen_tcp:listen
doesn't work (and it should totally work)


On 03/26, Dror Mein wrote:
> Hey all,
> I want to eunit+meck test a simple telnet client which uses the guard is_port(Port).
> how can I pass something in meck:expect/4 that will pass the guard?
> is it a record/reference that I can make up?
> can I use hd(erlang:ports()) -> #Port<0.0> without blowing up?
> Thanks

> _______________________________________________
> erlang-questions mailing list
> http://erlang.org/mailman/listinfo/erlang-questions

More information about the erlang-questions mailing list