<div dir="ltr">The last expression of a function is what that function returns. Are you trying to stream the file to the socket a line at a time, or are you trying to read the entire file and then write its contents to the socket en masse? For the former, you would simply have get_data take two arguments, a Device and Socket. When you read anything but eof from the device you would write the Data to Socket and then tail-recurse (`get_data(Device, Socket)` not a list). The eof case would simply return ok. For the latter case you could use your implementation of get_data as-is, and write its result to Socket.<div>
<div><br></div><div>Either way, there's not usually a need to use try/after to close a Port (such as a file or socket) in exceptional situations. The Port is linked to the process and will automatically be closed when the process dies. You should of course still close it in normal situations so that the file descriptor is reclaimed at the intended moment (perhaps immediately on eof).</div>
</div><div><br></div><div>-bob</div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Mon, Dec 16, 2013 at 9:48 AM, Ari King <span dir="ltr"><<a href="mailto:ari.brandeis.king@gmail.com" target="_blank">ari.brandeis.king@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div style="font-family:arial,sans-serif;font-size:13px">I've just started with Erlang and to learn/practice I'm attempting to put together a simple TCP server that reads data from a file and writes it to a socket. So far I've put together some code to read the data (combination of ascii, binary, control characters). But since (as I understand) Erlang doesn't have a return mechanism, how do I read a line of data and return it to be written to the socket? Right now, the code just recursively collects the data.<br>

<br>    -module(mock_tcp).<br>    -export([start/1]).<br>    <br>    start([Ip, Port, Filename]) -><br>      io:format("Server available at ~w on port ~w. Reading from ~w.", [Ip, Port, Filename]),<br>      {ok, Device) = file:open(Filename, read),<br>

      try get_data(Device)<br>        after file:close(Device)<br>      end.<br>    <br>    get_data(Device) -><br>      case io:get_line(Device) of<br>        {ok, Data} -> [Data | get_data(Device)];<br>        eof -> []<br>

      end.<br><br></div><span style="font-family:arial,sans-serif;font-size:13px">Thanks.</span><span class="HOEnZb"><font color="#888888"><br><div><span style="font-family:arial,sans-serif;font-size:13px"><br></span></div>
<div><span style="font-family:arial,sans-serif;font-size:13px">Ari</span></div>
</font></span></div>
<br>_______________________________________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br>
<a href="http://erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://erlang.org/mailman/listinfo/erlang-questions</a><br>
<br></blockquote></div><br></div>