Last active
August 29, 2015 14:01
-
-
Save maximvl/6b7588ddf16f849a9ef6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
result on client: | |
> free -m | |
total used free shared buffers cached | |
Mem: 7680 5190 2489 629 40 939 | |
-/+ buffers/cache: 4211 3469 | |
Swap: 0 0 0 | |
> erl | |
Erlang/OTP 17 [erts-6.0] [source] [64-bit] [smp:4:4] [async-threads:10] | |
Eshell V6.0 (abort with ^G) | |
1> sctp_client:client(). | |
Connection Successful, Assoc={sctp_assoc_change,comm_up,0,2,10,43} | |
readed: 365537 | |
{error,enomem}sended in: 222 | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(sctp_client). | |
%% create test file with dd if=/dev/urandom of=test.file bs=365537 count=1 | |
-export([client/0, client/1, client/2]). | |
-include_lib("kernel/include/inet.hrl"). | |
-include_lib("kernel/include/inet_sctp.hrl"). | |
client() -> | |
client([localhost]). | |
client([Host]) -> | |
client(Host, 2006); | |
client([Host, Port]) when is_list(Host), is_list(Port) -> | |
client(Host,list_to_integer(Port)), | |
init:stop(). | |
client(Host, Port) when is_integer(Port) -> | |
{ok,S} = gen_sctp:open(), | |
{ok,Assoc} = gen_sctp:connect | |
(S, Host, Port, | |
[{sctp_initmsg,#sctp_initmsg{num_ostreams=2}}, | |
{sndbuf, 4194304}]), | |
io:format("Connection Successful, Assoc=~p~n", [Assoc]), | |
{ok, Data} = file:read_file("test.file"), | |
io:format("readed: ~p~n", [byte_size(Data)]), | |
T = erlang:now(), | |
io:write(gen_sctp:send(S, Assoc, 0, Data)), | |
T2 = erlang:now(), | |
io:format("sended in: ~p~n", [timer:now_diff(T2, T)]), | |
io:nl(), | |
timer:sleep(5000), | |
io:write(gen_sctp:send(S, Assoc, 5, <<"Test 5">>)), | |
io:nl(), | |
timer:sleep(5000), | |
io:write(gen_sctp:abort(S, Assoc)), | |
io:nl(), | |
timer:sleep(1000), | |
gen_sctp:close(S). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(sctp_server). | |
-export([server/0,server/1,server/2]). | |
-include_lib("kernel/include/inet.hrl"). | |
-include_lib("kernel/include/inet_sctp.hrl"). | |
server() -> | |
server(any, 2006). | |
server([Host,Port]) when is_list(Host), is_list(Port) -> | |
{ok, #hostent{h_addr_list = [IP|_]}} = inet:gethostbyname(Host), | |
io:format("~w -> ~w~n", [Host, IP]), | |
server([IP, list_to_integer(Port)]). | |
server(IP, Port) when is_tuple(IP) orelse IP == any orelse IP == loopback, | |
is_integer(Port) -> | |
{ok,S} = gen_sctp:open(Port, [{recbuf,4194304}, {ip,IP}]), | |
io:format("Listening on ~w:~w. ~w~n", [IP,Port,S]), | |
ok = gen_sctp:listen(S, true), | |
inet:setopts(S, [{recbuf, 4194304}]), | |
server_loop(S). | |
server_loop(S) -> | |
case gen_sctp:recv(S) of | |
{error, Error} -> | |
io:format("SCTP RECV ERROR: ~p~n", [Error]); | |
{ok, {Ip, Port, Opts, Data}} when is_binary(Data) -> | |
file:write_file("sctp.txt", Data), | |
io:format("written: ~p~n", [byte_size(Data)]); | |
Data -> | |
io:format("Received: ~p~n", [Data]) | |
end, | |
server_loop(S). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment