Re: Talking to TCP server from shell

Magnus Leone <[email protected]>
Newsgroups gmane.comp.lang.erlang.general
Message-ID <CABz=PzPyybp7SgwhfNWfFK-bXtqvLL=6apKe=GV7cBE=QC_09A@mail.gmail.com>
Heh, I figured out why it didn't work. It was because the server was
running in a PowerShell on Windows, whereas the client side was running in
a WSL2 Linux shell on the same host. If I start both of them in either WSL2
or in PowerShell, then it works fine. Thanks for your help.

Cheers,
Magnus

On Fri, Nov 12, 2021 at 9:48 AM Christophe De Troyer <[email protected]>
wrote:

> I have a pastebin clone that accepts content over a socket (
> https://github.com/m1dnight/exbin/blob/main/lib/exbin/socket.ex). The way
> you do it there is `echo "my content" | nc example.com 9999`.
>
> I do recall that getting it to work was a bit of fiddling with the socket
> options..
>
> Magnus Leone wrote on 11/12/2021 2:24 AM:
>
> Hello,
>
> I'm working on making a very basic talk about networking, and eventually a
> webserver, but I thought I'd start with the very basic parts; just a TCP
> echo server. The thing is that the talk is not specifically about Erlang,
> I'm just using that as I'm most familiar with it (and hey, if I get some
> people interested, that's great). So I wanted the client side to be just a
> regular shell.
>
> Here is where my problem comes in - I can't seem to get the server process
> to accept the incoming connection when it comes from a shell. The code is
> pretty much straight out of Joe's book and it works fine as long as the
> other side is also an Erlang process. What I've tried so far:
>
> >telnet localhost 7777
> Trying 127.0.0.1...
> telnet: Unable to connect to remote host: Connection refused
>
> >netcat localhost 7777
> *no output*
>
> Does anyone know how I can use bash to connect to a TCP socket in this
> scenario?
>
> Cheers,
> Magnus
>
> start(Port) ->
>     {ok, ListenSocket} =
>         gen_tcp:listen(Port, [list, {packet, 0},
>                                     {reuseaddr, true},
>                                     {active, true}]),
>     {ok, Socket} = gen_tcp:accept(ListenSocket),
>     ok = gen_tcp:close(ListenSocket),
>     io:format("Server accepted connection on port ~p~n", [Port]),
>     loop(Socket).
>
> loop(Socket) ->
>     receive
>         {tcp, Socket, StringMsg} ->
>             io:format("Server received message: ~p~n", [StringMsg]),
>             Reply = "Echo " ++ StringMsg,
>             io:format("Server replying: ~p~n", [Reply]),
>             gen_tcp:send(Socket, Reply),
>             loop(Socket);
>         {tcp_closed, Socket} ->
>             io:format("Server socket closed - shutting down...~n")
>     end.
>
>
> --
> Sent from Postbox <https://www.postbox-inc.com>
>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.