For SO question 9547325

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
module Main where

import Control.Concurrent (forkIO, threadDelay)
import Control.Concurrent.STM
import Network
import Control.Monad
import System.IO
import qualified Data.Vector.Mutable as V

type Client = (Handle, HostName, PortNumber)
type DataVector = V.IOVector Int

clientPrompt :: Client -> String
clientPrompt (_, hname, port) = hname ++ ":" ++ show port ++ ">> "

createVector :: Int -> IO DataVector
createVector = V.new

main :: IO ()
main = withSocketsDo main'

main' :: IO ()
main' = createVector 100000000 >>= \v ->
        newTVarIO v >>= \tv ->
        listenOn (PortNumber (fromIntegral 1212)) >>= \socket ->
        forever (connect tv socket) >>
        putStrLn "Main thread done."

connect :: TVar DataVector -> Socket -> IO ()
connect tv socket = accept socket >>= \c@(handle, hostname, port) ->
                    putStrLn ("Accepting " ++ hostname ++ "(" ++ show port ++ ")" ) >>
                    forkIO (client tv c) >>
                    return ()

client :: TVar DataVector -> Client -> IO ()
client tv c@(handle, hname, port) = hIsEOF handle >>= \eof ->
                                    if eof
                                    then goodbye c
                                    else hGetLine handle >>= \line ->
                                        putStrLn (clientPrompt c ++ line) >>
                                        client tv c 


goodbye :: Client -> IO ()
goodbye c = putStrLn (clientPrompt c ++ "Bye bye.")
25:31: Error: Redundant fromIntegral
Found:
fromIntegral 1212
Why not:
1212