I’m interesting to send a file and it’s filename.
Server’s options:
-define(TCP_OPTIONS_SERVER, [binary, {packet, 0}, {active, false}]).
That’s the receiver loop:
file_receiver_loop(Socket,Bs)->
case gen_tcp:recv(Socket, 0) of
{ok, B} ->
file_receiver_loop(Socket, [Bs, B]);
{error, closed} ->
io:format("~nReceived!~n ~p",[Bs]),
save_file(Bs)
end.
I send file with:
file:sendfile(FilePath, Socket),
When I send file and filename
gen_tcp:send(Socket,Filename),
file:sendfile(FilePath, Socket),
The Binary Data has a variable structure.
Thanks all!
I make this code to solve the problem.
I send 30 byte for the file name.
If the filename<30 I use the padding with white char.
When the connection is accepted I call function file_name_receiver(Socket), that receive the filename:
This function riceive the binary data file:
Finally, this function save the file.
The sender use a simple function: