TCP performance

Chris Hicks <>
Fri Jun 18 03:41:22 CEST 2010

In designing the TCP server, which will handle all communication, for my project I based my design off of a couple of different tutorials, modified to suit my own needs. What I have is a gen_server that spawns a new process which waits for a connection on the single port of communication, when a connection is accepted this process tells the gen_server that it needs to spawn a new process and then it sends the socket off to another gen_server where the data will be grabbed and processed.
As far as I understand it once the socket connection has been accepted there should be no reason why another connection can't be established on that port immediately following. In other words the system doesn't care if the data has been received or not, when it comes to allowing another connection to take place, only that the first connection has been made. Is that true?
I ask because I ran a very simple test against the server which simply asked for a connection and then immediately closed it. Everything seems to work flawlessly except that making 1000 connections takes about 3.5 seconds and for my purposes that is a bit too long. The code for the server is below, and hopefully with a tip someone gave me earlier it will be formatted correctly:
init(_Args) ->    process_flag(trap_exit, true),    case gen_tcp:listen(54637, [binary, {packet, 0}, {reuseaddr, true}]) of        {ok, LSock} ->            State = #state{lsock = LSock},            {ok, accept(State)};        {error, Reason} ->            {stop, Reason}	end.			accept(State = #state{lsock=LSock}) ->    proc_lib:spawn(?MODULE, accept, [self(), LSock]),    State.	accept(Server, LSock) ->    case gen_tcp:accept(LSock) of        {ok, Socket} ->            gen_server:cast(Server, {accepted, self()}),            gen_server:cast(gateway_verifier, {connected, Socket});        {error, closed} ->            gen_server:cast(Server, {closed, self()})	end.
This is the handle_cast callback for the gateway_verifier gen_server:
handle_cast(Request, State) ->    case Request of        {connected, Socket} ->            {noreply, recv({State, Socket})};			        {'EXIT', _From, _Reason} ->            {noreply, State};        _Other ->            {noreply, State}	end.
And these are the functions which will start the processing of the data sent along the socket:
recv({State, Socket}) ->    proc_lib:spawn(?MODULE, recv, [self(), Socket]),	State.	recv(_Server, Sock) ->    case gen_tcp:recv(Sock) of        {ok, _Packet} ->            %% Do some fancy stuff here            ok;        {error, Reason} ->            {stop, Reason}	end.

This is my first real attempt at this so I wouldn't be surprised if there were some glaring inefficiencies in there. Any thoughts on how to increase the speed of accepting incoming connections? 		 	   		  
Hotmail is redefining busy with tools for the New Busy. Get more from your inbox.

More information about the erlang-questions mailing list