Parsing an incoming Shoutcast request

Joe Armstrong (AL/EAB) <>
Mon May 29 16:27:40 CEST 2006


This is covered in "Joe's spitting in the sawdust Erlang tutorials
Tutorial number 2"
(http://www.sics.se/~joe/tutorials/web_server/web_server.html)

You want to spawn (presumably) one handler/per connection - 

I have a module that does this:
  
   http://www.sics.se/~joe/tutorials/web_server/tcp_server.erl

tcp_server exports a number of useful things. Including:

	start_raw_server(Port, Fun, Max)

Which starts a listening server on <Port> which accepts a maximum of
<Max> connections.
Each connection is handled by <Fun>, which runs in a separate parallel
processes and communicates with the client.

I'll just quote from the comments at the start of the module

%% To setup a lister

%% start_server(Port) ->    
%%     S = self(),
%%     process_flag(trap_exit, true),
%%     tcp_server:start_raw_server(Port, 
%% 				fun(Socket) -> input_handler(Socket, S)
end, 
%% 				15,
%%                              0)
%%     loop().

%% The loop() process is a central controller that all
%% processes can use to synchronize amongst themselves if necessary
%% It ends up as the variable "Controller" in the input_handler

%% A typical server is written like this:

%% input_handler(Socket, Controller) ->
%%     receive
%% 	   {tcp, Socket, Bin} ->
%% 	      ...
%% 	      gen_tcp:send(Socket, ...)
%% 
%%   	  {tcp_closed, Socket} ->
%% 
%% 	Any ->
%% 	    ...
%% 
%%     end.

This input handler probably is what you want. One of these is
spawned/per connection.

The local variable Controller can be used to communicate with the master
process (ie the
process evaluating loop()).

tcp_server.erl is very useful - I use it for virtually every networking
application
that I write :-)


/Joe








 

> -----Original Message-----
> From:  
> [mailto:] On Behalf Of 
> Andrew Lentvorski
> Sent: den 29 maj 2006 13:58
> To: 
> Subject: Parsing an incoming Shoutcast request
> 
> Another newbie question.
> 
> I'm trying to do something very similar to the Shoutcast 
> server sample code from Practical Common Lisp.  The initial 
> request from an MP3 client to the streaming server (which I 
> want to write in Erlang) looks very much like HTTP.
> 
> GET / HTTP/1.1\r\n
> Host: 127.0.0.1:33669\r\n
> User-Agent: VLC media player - version 0.8.5 Janus - VideoLAN team\r\n
> Range: bytes=0-\r\n
> Icy-MetaData: 1\r\n
> Connection: Close\r\n
> \r\n
> 
> Now, I know have to do the gen_tcp:listen and gen_tcp:accept 
> to get the actual connection socket.
> 
> Once, I have that socket, though, I either need to do 
> something with messages and {active, once} or gen_tcp:recv() 
> and {active, false}.
> 
> I can't see a particularly obvious way to structure this for 
> pattern matches to make things easy.
> 
> Given that this looks like it should be very ripe for some 
> form of pattern match, I thought I would try asking those 
> with more experience for some friendly advice before plowing 
> ahead and writing lots of very stupid code.
> 
> Thanks,
> -a
> 



More information about the erlang-questions mailing list