0

Here is a simple server in Erlang. I am trying to spawn using spawn/3 but I have to resort to using spawn/1 in order to get it to work.

-module(server_by_name).
-export([start/0]).

start() ->
    {ok, Listen} = gen_tcp:listen(1234, [binary, {packet, 0}, {reuseaddr, true}, {active, true}]),
    io:format("Server running."),
    % --> Why doesn't this work in place of the line below? --> spawn(server_by_name, connect, [Listen]).
    spawn(fun() -> connect(Listen) end).

connect(Listen) ->
    {ok, Socket} = gen_tcp:accept(Listen),
    spawn(fun() -> connect(Listen) end),
    io:format("Connection accepted on ~p Pid ~p~n", [Socket, self()]),
    receive
        {tcp, Socket, Bin} ->
            Request = parse_request(Bin),
            io:format("File requested: ~p~n", [Request]),
            LoadHtml = file:read_file(Request),
            case LoadHtml of
                {ok, Reply} ->
                    Header = "HTTP/1.1 200 OK \r\n Content-Type:HTML\r\n\r\n",
                    ConnectCheck = gen_tcp:send(Socket, Header ++ Reply),
                    io:format("Connection?? ~p~n", [ConnectCheck]);
                {error, Reason} ->
                    io:format("Error in loading html file: ~n~p~n", [Reason]);
            end;
        {tcp_closed, Socket} ->
            io:format("Server socket closed~n")
    after 5000 ->
        io:format("Client unresponsive~n")
    end.

parse_request(Bin) when is_binary(Bin)->
    parse_request(binary_to_list(Bin));

parse_request("GET /" ++ RestStr) ->
    get_request([], RestStr);

parse_request([_H|T]) ->
    parse_request(T).

get_request(Request, [32|_RestStr]) ->
    lists:reverse(Request);

get_request(Request, [H|RestStr]) ->
    get_request([H|Request], RestStr);

get_request(Request, []) ->
    io:format("Unexpected result in server_by_name:get_request()"),
    list:reverse(Request).

I have included all the code so you can copy/paste it and try it yourself if need be. You just need to have a file that can be served in the same directory and request it by name. Obviously, it has no security precautions, etc.

GenericJam
  • 2,915
  • 5
  • 31
  • 33

1 Answers1

3

spawn/3 requires the function to be exported. Your solution is perfectly reasonable, but you could also export connect/1:

-export([connect/1]).  %% do not use, exported for spawn
legoscia
  • 39,593
  • 22
  • 116
  • 167
  • I am just a newbie at Erlang but as far as I understand in order to have hot-swapping of code, you need to use spawn/3 or spawn/4. Is there a way to have hot-swapping of code without making private functions public? – GenericJam Mar 15 '13 at 11:14
  • 1
    No, there isn't. The process will use the new version of the module if and when you make an "external" function call, either using `spawn/3` or `spawn/4`, or calling `my_module:function(something)` (as opposed to `function(something)`) in your code. – legoscia Mar 15 '13 at 12:13