v.yurin@hell:~$ telnet 127.0.0.1 59690
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
start
Some data before file
File 1:
%% @author Slava Yurin <YurinVV@ya.ru>
%% @doc Test Sendfile
-module(send_file_test).

-export([one_client/3]).
-export([start/0]).

start() ->
	{ok, Listen} = gen_tcp:listen(0, [{active, false}, {ip, {127, 0, 0, 1}}]),
	{ok, Port} = inet:port(Listen),
	io:format("Listen on ~p~n", [Port]),
	Listen.

one_client(Listen, Opt, FileCount) ->
	{ok, Client} = gen_tcp:accept(Listen),
	%Opt = [{delay_send, false}, {nodelay, true}],
	%Opt = [{delay_send, true}, {nodelay, false}],
	ok = inet:setopts(Client, [binary, {packet, line}, {active, false} | Opt]),
	Pid = spawn(fun() -> loop(Client, FileCount) end),
	ok = gen_tcp:controlling_process(Client, Pid),
	Pid ! start,
	ok.

loop(Client, FileCount) ->
	receive start -> ok end,
	{ok, <<"start\r\n">>} = gen_tcp:recv(Client, 0),
	{ok, FD} = file:open("send_file_test.erl", [raw, read, binary]),
	loop(Client, FD, FileCount).

loop(_, _, 0) ->
	ok;
loop(Client, FD, Pos) ->
	io:format("Send file ~p~n", [Pos]),
	ok = gen_tcp:send(Client,
		["Some data before file\r\nFile ", integer_to_list(Pos), ":\r\n"]),
	{ok, _} = file:sendfile(FD, Client, 0, 0, []),
	ok = gen_tcp:send(Client, ["File ", integer_to_list(Pos), " end\r\nSome data after file\r\n"]),
	loop(Client, FD, Pos - 1).
File 1 end
Some data after file
Connection closed by foreign host.
v.yurin@hell:~$ telnet 127.0.0.1 59690
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
start
%% @author Slava Yurin <YurinVV@ya.ru>
%% @doc Test Sendfile
-module(send_file_test).

-export([one_client/3]).
-export([start/0]).

start() ->
	{ok, Listen} = gen_tcp:listen(0, [{active, false}, {ip, {127, 0, 0, 1}}]),
	{ok, Port} = inet:port(Listen),
	io:format("Listen on ~p~n", [Port]),
	Listen.

one_client(Listen, Opt, FileCount) ->
	{ok, Client} = gen_tcp:accept(Listen),
	%Opt = [{delay_send, false}, {nodelay, true}],
	%Opt = [{delay_send, true}, {nodelay, false}],
	ok = inet:setopts(Client, [binary, {packet, line}, {active, false} | Opt]),
	Pid = spawn(fun() -> loop(Client, FileCount) end),
	ok = gen_tcp:controlling_process(Client, Pid),
	Pid ! start,
	ok.

loop(Client, FileCount) ->
	receive start -> ok end,
	{ok, <<"start\r\n">>} = gen_tcp:recv(Client, 0),
	{ok, FD} = file:open("send_file_test.erl", [raw, read, binary]),
	loop(Client, FD, FileCount).

loop(_, _, 0) ->
	ok;
loop(Client, FD, Pos) ->
	io:format("Send file ~p~n", [Pos]),
	ok = gen_tcp:send(Client,
		["Some data before file\r\nFile ", integer_to_list(Pos), ":\r\n"]),
	{ok, _} = file:sendfile(FD, Client, 0, 0, []),
	ok = gen_tcp:send(Client, ["File ", integer_to_list(Pos), " end\r\nSome data after file\r\n"]),
	loop(Client, FD, Pos - 1).
Connection closed by foreign host.
v.yurin@hell:~$ telnet 127.0.0.1 59690
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
start


[ ... skip ... ]


%% @author Slava Yurin <YurinVV@ya.ru>
%% @doc Test Sendfile
-module(send_file_test).

-export([one_client/3]).
-export([start/0]).

start() ->
	{ok, Listen} = gen_tcp:listen(0, [{active, false}, {ip, {127, 0, 0, 1}}]),
	{ok, Port} = inet:port(Listen),
	io:format("Listen on ~p~n", [Port]),
	Listen.

one_client(Listen, Opt, FileCount) ->
	{ok, Client} = gen_tcp:accept(Listen),
	%Opt = [{delay_send, false}, {nodelay, true}],
	%Opt = [{delay_send, true}, {nodelay, false}],
	ok = inet:setopts(Client, [binary, {packet, line}, {active, false} | Opt]),
	Pid = spawn(fun() -> loop(Client, FileCount) end),
	ok = gen_tcp:controlling_process(Client, Pid),
	Pid ! start,
	ok.

loop(Client, FileCount) ->
	receive start -> ok end,
	{ok, <<"start\r\n">>} = gen_tcp:recv(Client, 0),
	{ok, FD} = file:open("send_file_test.erl", [raw, read, binary]),
	loop(Client, FD, FileCount).

loop(_, _, 0) ->
	ok;
loop(Client, FD, Pos) ->
	io:format("Send file ~p~n", [Pos]),
	ok = gen_tcp:send(Client,
		["Some data before file\r\nFile ", integer_to_list(Pos), ":\r\n"]),
	{ok, _} = file:sendfile(FD, Client, 0, 0, []),
	ok = gen_tcp:send(Client, ["File ", integer_to_list(Pos), " end\r\nSome data after file\r\n"]),
	loop(Client, FD, Pos - 1).
Connection closed by foreign host.
v.yurin@hell:~$ telnet 127.0.0.1 59690
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
start


[ ... skip ... ]


%% @author Slava Yurin <YurinVV@ya.ru>
%% @doc Test Sendfile
-module(send_file_test).

-export([one_client/3]).
-export([start/0]).

start() ->
	{ok, Listen} = gen_tcp:listen(0, [{active, false}, {ip, {127, 0, 0, 1}}]),
	{ok, Port} = inet:port(Listen),
	io:format("Listen on ~p~n", [Port]),
	Listen.

one_client(Listen, Opt, FileCount) ->
	{ok, Client} = gen_tcp:accept(Listen),
	%Opt = [{delay_send, false}, {nodelay, true}],
	%Opt = [{delay_send, true}, {nodelay, false}],
	ok = inet:setopts(Client, [binary, {packet, line}, {active, false} | Opt]),
	Pid = spawn(fun() -> loop(Client, FileCount) end),
	ok = gen_tcp:controlling_process(Client, Pid),
	Pid ! start,
	ok.

loop(Client, FileCount) ->
	receive start -> ok end,
	{ok, <<"start\r\n">>} = gen_tcp:recv(Client, 0),
	{ok, FD} = file:open("send_file_test.erl", [raw, read, binary]),
	loop(Client, FD, FileCount).

loop(_, _, 0) ->
	ok;
loop(Client, FD, Pos) ->
	io:format("Send file ~p~n", [Pos]),
	ok = gen_tcp:send(Client,
		["Some data before file\r\nFile ", integer_to_list(Pos), ":\r\n"]),
	{ok, _} = file:sendfile(FD, Client, 0, 0, []),
	ok = gen_tcp:send(Client, ["File ", integer_to_list(Pos), " end\r\nSome data after file\r\n"]),
	loop(Client, FD, Pos - 1).
