Develop and Explore

Quick & Easy TCP GenServer with Elixir & Erlang

TCP/IP server implementation

Malreddy Ankanna
blackode
Published in
5 min readJul 2, 2017

--

Photo by Taylor Vick on Unsplash

This article is all about writing your own TCP server from the scratch. You will see the implementation of GenServer receiving and sending packets inside the TCP server. We go from a very basic step, creating a mix project .

Let’s dive into action.

For your confirmation this is not about TCP basics. If you are expecting about the TCP then it is not for you and I guess you already knew how to create a mix project in elixir, if not then look down else skip the para.

Mix Project Creation

mix new gen_tcp --module TcpServer

The above command will generate a mix project with a file gen_tcp.ex where you can see the module name will be TcpServer instead of GenTcp . If you want you can also change the filename gen_tcp.ex to tcp_server.ex . It is much sensible to keep the module name and file name like/same.

Now, I am renaming the file /lib/gen_tcp.ex to /lib/tcp_server.ex as my module is TcpServer

We are using the Eralng module called gen_tcp here. This module comprises of functions for TCP/IP protocol communication with sockets.

GenServer Behavior…

--

--

Malreddy Ankanna
blackode

Programmer & Writer, I write about coding, thoughts, ideas, personal musings, technical articles, and tutorials.https://bio.link/blackode