a
554325746@qq.com
2019-12-25 603cb36a5123e46656b06a5deb8d7ac7ff81307f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
%% Copyright
-module(socket_server).
-author("Ola").
 
%% API
-export([start_server/0]).
-define(Port,9000).
 
start_server()->
  Pid = spawn_link(fun() ->
    {ok,LSocket} = gen_tcp:listen(?Port,[binary,{active,false}]),
    spawn(fun() -> acceptState(LSocket) end),
    timer:sleep(infinity)
    end),
  {ok, Pid}.
 
acceptState(LSocket)->
  {ok, ASocket} = gen_tcp:accept(LSocket),
  spawn(fun() -> acceptState(LSocket) end),
  handler(ASocket).
 
handler(ASocket) ->
  inet:setopts(ASocket,[{active,once}]),
  receive
    {tcp,ASocket,<<"quit">>} ->
      gen_tcp:close(ASocket);
    {tcp,ASocket,<<"value=",X/binary>>} ->
      Val = list_to_integer(binary_to_list(X)),
      Return = Val * Val,
      gen_tcp:send(ASocket,"Result Sq: "++list_to_binary(integer_to_list(Return))),
      handler(ASocket);
    {tcp,ASocket,BinaryMsg} ->
      if
        (BinaryMsg =:= <<"UXLearner">>) ->
          gen_tcp:send(ASocket,"A for UX");
        true ->
          gen_tcp:send(ASocket,"Everything else")
      end,
      handler(ASocket)
  end.