I've created a server with no client code. I just have to type on the terminal telnet 127.0.0.1 4001 and on an other terminal telnet 127.0.0.2 4001 so when I type a message on the first terminal it appears on the same terminal, I know this an echo server, all I want is to modify this code if it's possible so the other client on other terminal could receive the message. This is an echo server code:
defmodule Multichat.Server do
require Logger
def accept(port) do
{:ok, socket} = :gen_tcp.listen(port, [:binary, packet: :line, active: false, reuseaddr: true])
Logger.info "Accepting connections on port #{port}"
loop_acceptor(socket)
end
defp loop_acceptor(socket) do
{:ok, client} = :gen_tcp.accept(socket)
{:ok, pid} = Task.Supervisor.start_child(Multichat.Server.TaskSupervisor, fn -> serve(client) end)
:ok = :gen_tcp.controlling_process(client, pid)
loop_acceptor(socket)
end
defp serve(socket) do
socket
|> read_line()
|> write_line(socket)
serve(socket)
end
defp read_line(socket) do
{:ok, data} = :gen_tcp.recv(socket, 0)
data
end
defp write_line(line, socket) do
:gen_tcp.send(socket, line)
end
end
What should I change so when I open many clients using telnet they recieve messages from one another