Home > English, Erlang > Serving multiple clients on the same port in Erlang

Serving multiple clients on the same port in Erlang

In the previous post, I wrote down a really basic interface for the gen_server I’m going to use in my card game: there were a couple of basic handle_calls used to manage users’ turns etc etc.

Obviously, if we want to handle connecting users, we must set up a socket on a specific port and listen to what is coming in there.

A really easy way to do this is shown in this post. Let’s start by writing in our gen_server (in the internal functions space) the following function:

conn_manager(Port) ->
    {ok, Listen} = gen_tcp:listen(Port, [binary,
                                                    {keepalive, true},
                                                    {reuseaddr, true},
                                                    {active, once}]),
    spawn(fun() -> parallel_connection(Listen) end).

conn_manager(Port) is a really simple fuction called in the init function; it does the following: create a tcp socket on the Port passed as argument to the function, this socket will have some features, as you can see we specified that the traffic going trough it is of type binary, that we want the server to send keepalive packets to mantain the connection up and running, that the address may be reused and most importan that this socket will be active once.

What does the active once mean? Well, the socket could have been always active, and this means that we would have been listening to the incoming data without any restrictions…well, sometimes this is a good thing, but think about Denial of Service attacks..we definitely may prefer to listen to the incoming traffic only when neededt…

After the creation of the socket we spawn a new process and pass to it the reference to the socket.

Let’s move on:

parallel_connection(Listen) ->
    {ok, Socket} = gen_tcp:accept(Listen),
    case is_in_blacklist(Socket) of
	false ->
	    spawn(fun() -> parallel_connection(Listen) end),
	true ->

parallel_connection(Listen) is the function spawned as a new process by the previous function. In the very beginning it accepts any incoming connection and checks whether it comes from a blacklisted IP (note that is_in_blacklist is function that I did by myself so it is not a erlang standard function, if the clients in in the blacklist the connection is closed, otherwise we do two things: first we spawn a new parallel_connection function so that new clients may be able to connect to the server, and the we call the function conn_loop(Socket) so that we can manage the traffic coming from the already connected user.

conn_loop(Socket) ->
	{tcp, Socket, Bin} ->
	    inet:setopts(Socket, [{active, once}]),
	    case Bin of
                 ....case matching with incoming binaries (e.g. join match)
	{tcp_closed, Socket} ->

As you can see conn_loop(Socket) is a very basic function…it is a simple receive function that in case of incoming traffic {tcp, Socket, Bin} triggers the socket to receive once more and than with a case construct manages the incoming binaries.

This post was written in a hurry, so if you have questions or comments or anything just comment it or mail me so that I can correct it!

Categories: English, Erlang Tags: , ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: