Beginner: Unexplained network behavior

Aaron Hunter <>
Fri Apr 23 17:23:03 CEST 2004


Hello,

A long time Java developer I recently discovered Erlang and am very 
impressed. With a few improvements Erlang could very well be a J2EE killer.

I wrote this simple module to explore how sockets work in Erlang but am 
puzzled by its behavior. It is supposed to scan 254 IP's simultaneously. 
looking for HTTP servers. Over the course of several runs, however, it 
gives me different answers and often misses HTTP servers.

Can someone point out what I am doing wrong?
Thank you.

-module( scanner ).
-export( [scan_http/1, scan_port/3] ).

% Check for an open socket on the given port
scan_port( PID, IP, Port ) ->
	case gen_tcp:connect( IP, Port, [], 5000)  of
		{ ok, Socket } ->
			gen_tcp:close( Socket ),
			PID ! { found, IP, Port };
		{ error, _ } ->
			PID ! { none, IP }
	end.

% Wait for all 254 processes to finish
wait( 254, Found ) ->
	io:fwrite("~nFound ~B HTTP servers: ~p~n", [ length( Found ), Found ]);
wait( Count, Found ) ->
	receive
		{ found, IP, Port } ->
			io:fwrite("+"),
			wait( Count + 1, [{IP,Port}|Found] );
		{ none, IP } ->
			io:fwrite("."),
			wait( Count + 1, Found )
	end.

% Call with a network IP tuple in the form {192,168,0,0}
scan_http( {A,B,C,D} ) ->
	io:fwrite("Scanning ~n"),
	lists:foreach( fun(Host) -> spawn( ?MODULE, scan_port, [ self(), 
{A,B,C,Host}, 80 ] ) end, lists:seq(1,254) ),
	wait( 0, [] ).








More information about the erlang-questions mailing list