[erlang] gen_tcp传输文件原型(二进制)

 1 -module(tcp).
 2 -export([client/3,server/1]).
 3 
 4 client(Port,SiteID,Deploy) ->
 5     Host = "localhost",
 6     DeployBin = list_to_binary(Deploy),
 7     {ok,Sock} = gen_tcp:connect(Host,Port,[binary,{packet,4}]),
 8     ok = gen_tcp:send(Sock,<<SiteID:32,DeployBin/binary>>),
 9     receive_data(Sock,[],SiteID,Deploy).
10 
11 receive_data(Socket,SoFar,SiteID,Deploy) ->
12     receive
13         {tcp,Socket,<<"error",Msg/binary>>} ->
14             io:format("~p~p~p~n",[binary_to_list(Msg),SiteID,Deploy]);
15         {tcp,Socket,<<"success",Data/binary>>} ->
16             Target = "/tmp/"++ integer_to_list(SiteID) ++ "-" ++ Deploy ++ ".tar",
17             case file:write_file(Target,Data) of
18                 ok             -> ok;
19                 {error,Reason} -> io:format("~p~n",[Reason])
20             end;
21         {tcp,Socket,_Bin} ->
22             %%receive_data(Socket,[Bin|SoFar],SiteID,Deploy);
23             io:format("test");
24         {tcp_closed,Socket} ->
25 %            case lists:reverse(SoFar) of
26 %                <<"error",Msg/binary>>  ->
27 %                    {error,binary_to_list(Msg)};
28 %                <<"success",Data/binary>> ->
29 %                    Target = "/tmp/"++ SiteID ++ "-" ++ Deploy ++ ".tar",
30 %                    file:write_file(Target,list_to_binary(Data),
31 %                    {ok,Target};
32 %                _                   ->
33 %                    {error,unknown}
34 %            end
35             io:format("~p~n",[SoFar])
36     end.
37 
38 server(Port) ->
39     case gen_tcp:listen(Port,[binary,{packet,4}, %% 每个应用程序消息都从一个4字节长的头部开始
40                                        {reuseaddr,true},  %% 
41                                        {active,true}]) of
42         {ok,Listen} ->
43             spawn(fun()-> connect(Listen) end);
44         {error,Why} ->
45             io:format("~p~n",[Why])
46     end.
47 
48 connect(Listen) ->
49     case gen_tcp:accept(Listen) of %% 等待暂停并等待一个连接
50         {ok,Socket} ->
51             spawn(fun() -> connect(Listen) end),
52             loop(Socket);
53         {error,Why} ->
54             io:format("~p~n",[Why])
55     end.
56 
57 loop(Socket) ->
58     receive
59         %% 传入正确的二进制格式
60         {tcp,Socket,<<SiteID:32,DeployBin/binary>>} ->
61             Deploy = binary_to_list(DeployBin),
62             Filename = "/tmp/scm/" ++ integer_to_list(SiteID) ++ "-" ++ Deploy ++ ".tar",
63             T = case file:read_file(Filename) of
64                 {ok,Bin1} ->
65                     <<"success",Bin1/binary>>;
66                 {error,Reason} ->
67                     RBin = list_to_binary(atom_to_list(Reason)),
68                     <<"error",RBin/binary>>
69             end,
70             gen_tcp:send(Socket,T),
71             gen_tcp:close(Socket);
72         {tcp,Socket,_Bin}   ->
73             Msg = list_to_binary("illegal format"),
74             gen_tcp:send(Socket,[<<"error",Msg/binary>>]),
75             gen_tcp:close(Socket);
76         {tcp_closed,Socket} ->
77             io:format("Server socket closed~n")
78     end.

http://www.cnblogs.com/bluefrog/archive/2012/09/10/2679040.html 改进版加入了一些二进制处理

改一改还能写个二进制包处理的游戏服务器, 就可以开搞游戏了... 慢慢来咯

posted on 2012-09-14 16:22  bluefrog  阅读(997)  评论(0编辑  收藏  举报