Skip to content

Instantly share code, notes, and snippets.

@satom99
Last active December 30, 2017 17:23
Show Gist options
  • Save satom99/ef9333e5a9bb5dea5ce77d322847d8bf to your computer and use it in GitHub Desktop.
Save satom99/ef9333e5a9bb5dea5ce77d322847d8bf to your computer and use it in GitHub Desktop.
Joseph#8035's curiosity
defmodule Socket do
use GenServer
alias Socket.Worker
def start_link do
GenServer.start_link __MODULE__, []
end
def init(_state) do
options = [
:binary,
packet: :http,
active: false,
ip: {127, 0, 0, 1}
]
{:ok, socket} = :gen_tcp.listen(1337, options)
send(self(), :loop)
{:ok, socket}
end
def handle_info(:loop, socket) do
{:ok, client} = :gen_tcp.accept(socket)
Logger.debug "Accepting connection"
Worker.start_link(client)
send(self(), :loop)
{:noreply, socket}
end
end
defmodule Socket.Worker do
use GenServer
def start_link(client) do
GenServer.start_link __MODULE__, client
end
def init(client) do
options = [
active: true
]
Port.connect(client, self())
:inet.setopts(client, options)
{:ok, client}
end
def handle_info(message, client) do
IO.inspect message
{:noreply, client}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment