Skip to content

Commit cea5a19

Browse files
committed
Fixed TCP and UDP examples to print IP addresses and port numbers in a more readable way.
1 parent aa3b77a commit cea5a19

File tree

4 files changed

+17
-16
lines changed

4 files changed

+17
-16
lines changed

erlang/tcp_client/src/tcp_client.erl

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,20 @@ start() ->
2626
ok = maybe_start_network(atomvm:platform()),
2727
Host = maps:get(host, config:get()),
2828
Port = maps:get(port, config:get()),
29+
io:format("Attempting to connect to host ~p on port ~p ...~n", [Host, Port]),
2930
case gen_tcp:connect(Host, Port, []) of
3031
{ok, Socket} ->
31-
io:format("Connected to ~p from ~p~n", [peer_address(Socket), local_address(Socket)]),
32+
io:format("Connected to ~s from ~s~n", [peer_address(Socket), local_address(Socket)]),
3233
loop(Socket);
3334
Error ->
3435
io:format("An error occurred connecting: ~p~n", [Error])
3536
end.
3637

3738
loop(Socket) ->
38-
SendPacket = <<"AtomVM rocks!">>,
39+
SendPacket = <<"AtomVM">>,
3940
case gen_tcp:send(Socket, SendPacket) of
4041
ok ->
41-
io:format("Sent ~p to ~p\n", [SendPacket, peer_address(Socket)]),
42+
io:format("Sent ~p to ~s\n", [SendPacket, peer_address(Socket)]),
4243
receive
4344
{tcp_closed, Socket} ->
4445
io:format("Connection closed.~n"),
@@ -47,7 +48,7 @@ loop(Socket) ->
4748
io:format("TCP error ~p.~n", [Error]),
4849
ok;
4950
{tcp, Socket, ReceivedPacket} ->
50-
io:format("Received ~p from ~p~n", [ReceivedPacket, peer_address(Socket)]),
51+
io:format("Received ~p from ~s~n", [ReceivedPacket, peer_address(Socket)]),
5152
timer:sleep(1000),
5253
loop(Socket);
5354
Other ->
@@ -76,7 +77,7 @@ maybe_start_network(esp32) ->
7677
case network:wait_for_sta(Config, 30000) of
7778
{ok, {Address, Netmask, Gateway}} ->
7879
io:format(
79-
"Acquired IP address: ~p Netmask: ~p Gateway: ~p~n",
80+
"Acquired IP address: ~s Netmask: ~s Gateway: ~s~n",
8081
[to_string(Address), to_string(Netmask), to_string(Gateway)]
8182
),
8283
ok;

erlang/tcp_server/src/tcp_server.erl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ start() ->
2727
Port = maps:get(port, config:get()),
2828
case gen_tcp:listen(Port, []) of
2929
{ok, ListenSocket} ->
30-
io:format("Listening on ~p.~n", [local_address(ListenSocket)]),
30+
io:format("Listening on ~s.~n", [local_address(ListenSocket)]),
3131
spawn(fun() -> accept(ListenSocket) end),
3232
timer:sleep(infinity);
3333
Error ->
@@ -38,7 +38,7 @@ accept(ListenSocket) ->
3838
io:format("Waiting to accept connection...~n"),
3939
case gen_tcp:accept(ListenSocket) of
4040
{ok, Socket} ->
41-
io:format("Accepted connection. local: ~p peer: ~p~n", [
41+
io:format("Accepted connection. local: ~s peer: ~s~n", [
4242
local_address(Socket), peer_address(Socket)
4343
]),
4444
spawn(fun() -> accept(ListenSocket) end),
@@ -57,7 +57,7 @@ echo(Socket) ->
5757
io:format("TCP error ~p.~n", [Error]),
5858
ok;
5959
{tcp, Socket, Packet} ->
60-
io:format("Received packet ~p from ~p. Echoing back...~n", [
60+
io:format("Received packet ~p from ~s. Echoing back...~n", [
6161
Packet, peer_address(Socket)
6262
]),
6363
gen_tcp:send(Socket, Packet),
@@ -85,7 +85,7 @@ maybe_start_network(esp32) ->
8585
case network:wait_for_sta(Config, 30000) of
8686
{ok, {Address, Netmask, Gateway}} ->
8787
io:format(
88-
"Acquired IP address: ~p Netmask: ~p Gateway: ~p~n",
88+
"Acquired IP address: ~s Netmask: ~s Gateway: ~s~n",
8989
[to_string(Address), to_string(Netmask), to_string(Gateway)]
9090
),
9191
ok;

erlang/udp_client/src/udp_client.erl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,19 @@ start() ->
2626
ok = maybe_start_network(atomvm:platform()),
2727
case gen_udp:open(0) of
2828
{ok, Socket} ->
29-
io:format("Opened UDP socket on ~p.~n", [local_address(Socket)]),
29+
io:format("Opened UDP socket on ~s.~n", [local_address(Socket)]),
3030
loop(Socket);
3131
Error ->
3232
io:format("An error occurred opening UDP socket: ~p~n", [Error])
3333
end.
3434

3535
loop(Socket) ->
36-
Packet = <<"AtomVM rocks!">>,
36+
Packet = <<"AtomVM">>,
3737
Host = maps:get(host, config:get()),
3838
Port = maps:get(port, config:get()),
3939
case gen_udp:send(Socket, Host, Port, Packet) of
4040
ok ->
41-
io:format("Sent ~p to ~p:~p~n", [Packet, Host, Port]);
41+
io:format("Sent ~p to ~s~n", [Packet, to_string({Host, Port})]);
4242
Error ->
4343
io:format("An error occurred sending a packet: ~p~n", [Error])
4444
end,
@@ -59,7 +59,7 @@ maybe_start_network(esp32) ->
5959
case network:wait_for_sta(Config, 30000) of
6060
{ok, {Address, Netmask, Gateway}} ->
6161
io:format(
62-
"Acquired IP address: ~p Netmask: ~p Gateway: ~p~n",
62+
"Acquired IP address: ~s Netmask: ~s Gateway: ~s~n",
6363
[to_string(Address), to_string(Netmask), to_string(Gateway)]
6464
),
6565
ok;

erlang/udp_server/src/udp_server.erl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ start() ->
2727
Port = maps:get(port, config:get()),
2828
case gen_udp:open(Port, [{active, true}]) of
2929
{ok, Socket} ->
30-
io:format("Opened UDP socket on ~p.~n", [local_address(Socket)]),
30+
io:format("Opened UDP socket on ~s.~n", [local_address(Socket)]),
3131
loop();
3232
Error ->
3333
io:format("An error occurred opening UDP socket: ~p~n", [Error])
@@ -37,7 +37,7 @@ loop() ->
3737
io:format("Waiting to receive data...~n"),
3838
receive
3939
{udp, _Socket, Address, Port, Packet} ->
40-
io:format("Received UDP packet ~p from ~p~n", [Packet, to_string({Address, Port})])
40+
io:format("Received UDP packet ~p from ~s~n", [Packet, to_string({Address, Port})])
4141
end,
4242
loop().
4343

@@ -55,7 +55,7 @@ maybe_start_network(esp32) ->
5555
case network:wait_for_sta(Config, 30000) of
5656
{ok, {Address, Netmask, Gateway}} ->
5757
io:format(
58-
"Acquired IP address: ~p Netmask: ~p Gateway: ~p~n",
58+
"Acquired IP address: ~s Netmask: ~s Gateway: ~s~n",
5959
[to_string(Address), to_string(Netmask), to_string(Gateway)]
6060
),
6161
ok;

0 commit comments

Comments
 (0)