:gen_tcp doesn't work even though netcat does?

I’m running the fluidsynth server and want to send noteon events from elixir to the fluidsynth process.
I can do

$ nc localhost 8000
> noteon 0 40 100

And I hear a sound being played… when I use :gen_tcp I do not see the same results:

iex(10)> {:ok, sock} = :gen_tcp.connect({127,0,0,1}, 8000, [:binary, active: true])
{:ok, #Port<0.7>}
iex(11)> :gen_tcp.send(sock, "noteon 0 40 100")
:ok

… nothing gets played though. I’m guessing I have some option incorrect? I need to use something other than :binary maybe since these are plain strings?

Does the protocol expect lines that are separated by newline \n? Netcat sends the newline character over (if you press enter) and the Elixir code sends the string but not the newline.

Does sending "noteon 0 40 100\n" from Elixir work? That’s one difference I can think of.

If you need to receive line-based data see also the Erlang docs for inet:setopts, specifically {packet, line}. It only applies to received packets.

1 Like

Without knowing anything about fluidsynth, that looks like it might be a line-based tcp protocol given the lack of size headers or the like.

Have you tried adding a ‘\n’ or ‘\r\n’ to the end? Netcat is probably sending them implicitly as you press enter.

2 Likes

Yup that was it thank you!