我有一个客户端和一个服务器,我使用客户端发送一个字符串,例如"Hello"
它在服务器端显示为<<"Hello">>
. 有没有办法简单地转换<<"Hello">>
回"Hello"
? 我尝试使用,
String = io_lib:format("~p",[StringIn]),
lists:flatten(String),
但这使得格式<<"Hello">>
变成了[[60,60,"\"Hello\"",62,62]]
......
顺便说一下代码:
-module(ss1).
-compile(export_all).
-import(lists, [reverse/1]).
client() ->
{ok, Socket} = gen_tcp:connect("localhost", 2345,[binary, {packet, 4}]),
ok = gen_tcp:send(Socket, "Hello"),
receive
{tcp,Socket,String} ->
io:format("Client received = ~p~n",[String]),
io:format("Client result = ~p~n",[String]),
gen_tcp:close(Socket)
end.
server() ->
{ok, Listen} = gen_tcp:listen(2345, [binary, {packet, 4}, {reuseaddr, true}, {active, true}]),
{ok, Socket} = gen_tcp:accept(Listen),
gen_tcp:close(Listen),
loop(Socket).
loop(Socket) ->
receive
{tcp,Socket,String} ->
io:format("Server received String = ~p~n",[String]),
io:format("Server replying String = ~p~n",[String]),
gen_tcp:send(Socket, String),
loop(Socket);
{tcp_closed, Socket} ->
io:format("Server socket closed~n")
end.