November 21, 2008

Erlang, Apartment Hunting and the Job Search.

The last few days, since my departure at Contegix, I have been busy working on finding Erin and I an apartment, looking for gainful employment and working on my little Erlang project(s). Luckily all three went really well. I will hopefully be posting the Erlang stuff next week. Two weeks until the wedding and just a few more days than that until we head to Seattle. Things are a’changing.

November 12, 2008

A Simple Concurrent Erlang TCP Server.

The last few days I have been playing with gen_tcp and writing a simple TCP server. I have found a number of resources helpful: gen_tcp, miniserv, 20bits and Programming Erlang. My server will run and display the data sent to it along with the client IP address and a timestamp. The server will also echo back whatever the client sent. It uses {active, once} for flow control as well. I just used telnet as my client. Pretty simple but a good exercise to see how this stuff works.

erl_tcp.erl

-module(erl_tcp).
-export([start_server/0, connect/1, recv_loop/1]).

-define(LISTEN_PORT, 9000).
-define(TCP_OPTS, [binary, {packet, raw}, {nodelay, true}, {reuseaddr, true}, {active, once}]).

start_server() ->
% start up the service and error out if we cannot
case gen_tcp:listen(?LISTEN_PORT, ?TCP_OPTS) of
{ok, Listen} -> spawn(?MODULE, connect, [Listen]),
io:format(“~p Server Started.~n”, [erlang:localtime()]);
Error ->
io:format(“Error: ~p~n”, [Error])
end.

connect(Listen) ->
{ok, Socket} = gen_tcp:accept(Listen),
inet:setopts(Socket, ?TCP_OPTS),
% kick off another process to handle connections concurrently
spawn(fun() -> connect(Listen) end),
recv_loop(Socket),
gen_tcp:close(Socket).

recv_loop(Socket) ->
% reset the socket for flow control
inet:setopts(Socket, [{active, once}]),
receive
% do something with the data you receive
{tcp, Socket, Data} ->
io:format(“~p ~p ~p~n”, [inet:peername(Socket), erlang:localtime(), Data]),
gen_tcp:send(Socket, “I Received ” ++ Data),
recv_loop(Socket);
% exit loop if the client disconnects
{tcp_closed, Socket} ->
io:format(“~p Client Disconnected.~n”, [erlang:localtime()])
end.

Hopefully all of that made some sense. Here is an example of usage, first starting the server and receiving a message.

[zeusfaber@der-dieb ebin]$ erl
Erlang (BEAM) emulator version 5.6.5 [source] [64-bit] [smp:2] [async-threads:0] [hipe] [kernel-poll:false]

Eshell V5.6.5 (abort with ^G)
1> erl_tcp:start_server().
{{2008,11,12},{11,20,28}} Server Started.
ok
{ok,{{127,0,0,1},48940}} {{2008,11,12},{11,20,35}} <<"TEST123\r\n">>
2>

To send the message “TEST123″ I simply used telnet. Of course you could also write a client in Erlang to do the same.

[zeusfaber@der-dieb ~]$ telnet localhost 9000
Trying 127.0.0.1…
Connected to localhost.
Escape character is ‘^]’.
TEST123
I Received TEST123

November 5, 2008

Erlang R12B-5 is out.

Downloads are available here.

The highlights according to the readme:

OTP-7531 Processes spawned using proc_lib (including gen_server and
other library modules that use proc_lib) no longer keep the
entire argument list for the initial call, but only the
arity.

Also, if proc_lib:spawn/1 is used to spawn a fun, the actual
fun is not kept, but only module, function name, and arity of
the function that implements the fun.

The reason for the change is that keeping the initial fun (or
a fun in an argument list), would prevent upgrading the code
for the module. A secondary reason is that keeping the fun
and function arguments could waste a significant amount of
memory.

The drawback with the change is that the crash reports will
provide less precise information about the initial call (only
Module:Function/Arity instead of Module:Function(Arguments)).
The function proc_lib:initial_call/1 still returns a list,
but each argument has been replaced with a dummy atom.

OTP-7586 In user-defined attributes, Name/Arity is now allowed and
will be translated to {Name,Arity}. (An implementation of
EEP-24 by Richard O’Keefe.)

The module_info/{0,1} functions automatically inserted into
each compiled modules are now documented in the Modules
section in the Reference Manual.

OTP-7630 The eunit application (for unit testing of Erlang modules) by
Richard Carlsson is now included in OTP

November 4, 2008

More Music Videos.

Excepter – Any and Every

YouTube Preview Image