I have a basic client server in erlang which uses tcp.
How does one send the actual binary data from a file to the client who requested it?
How is the file sent in pieces?
I have this code
{ok, Socket} =
gen_tcp:connect({Ip}, 2345,
[binary, {packet, 4}]),
Does this {packet, 4} handle the data size that is send both ways?
Also, how does the client receive data and then do something with it? Like save it to a file?
Thanks
Yes –
{packet, 4}will cause erlang to require a packet frame of a 4 byte unsigned big endian integer length value on receive, and will emit one before each packet of data sent.You can send data on the socket by calling
gen_tcp:send(Socket, Data). This will do something like:So provided your file is less than 4Gb, you could send it by doing
On the receiving end:
You’ll get the complete file because of the
{packet, 4}framing.