title: Closing socket and handle in a client-server application
body: <p>This question refers to the <code>Network</code> library in Haskell.</p>
  
  <p>I have a simple client-server application. The server acquires a socket, accepts connections there, and forks a server thread to handle the requests:</p>
  
  <pre><code>sock &lt;- listenOn port
  (handle, _, _) &lt;- accept socket
  forkFinally (serve handle) (_ -&gt; hClose handle)
  -- Upon termination of the main thread I perform a 'sClose sock' as well
  </code></pre>
  
  <p>The client connects to the server, and reads and writes to the handle (as the server does):</p>
  
  <pre><code>handle &lt;- connectTo host port
  -- Read and write on 'handle'
  </code></pre>
  
  <p>Now my question is whether the client should close the handle as well when it finishes interacting with the server.</p>
  
  <p>Secondly, the server thread terminates when an <code>"end"</code> string is received, but it sends a message <code>"bye"</code> before closing the handle. Looking at <a href="https://stackoverflow.com/questions/17667901/how-to-handle-socket-read-in-c-when-the-remote-server-closes-socket-possibly-bef">similar questions</a>, it seems closing the handle at the server still allows the client to read all the data sent to it. My tests confirm this, but maybe I got just lucky. Will closing the handle at the server allow the client to read the data in all cases? </p>