Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
25
Task/Echo-server/Haskell/echo-server-1.hs
Normal file
25
Task/Echo-server/Haskell/echo-server-1.hs
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
module Main where
|
||||
import Network (withSocketsDo, accept, listenOn, sClose, PortID(PortNumber))
|
||||
import Control.Monad (forever)
|
||||
import System.IO (hGetLine, hPutStrLn, hFlush, hClose)
|
||||
import System.IO.Error (isEOFError)
|
||||
import Control.Concurrent (forkIO)
|
||||
import Control.Exception (bracket)
|
||||
|
||||
-- For convenience in testing, ensure that the listen socket is closed if the main loop is aborted
|
||||
withListenOn port body = bracket (listenOn port) sClose body
|
||||
|
||||
echo (handle, host, port) = catch (forever doOneLine) stop where
|
||||
doOneLine = do line <- hGetLine handle
|
||||
print (host, port, init line)
|
||||
hPutStrLn handle line
|
||||
hFlush handle
|
||||
stop error = do putStrLn $ "Closed connection from " ++ show (host, port) ++ " due to " ++ show error
|
||||
hClose handle
|
||||
|
||||
main = withSocketsDo $
|
||||
withListenOn (PortNumber 12321) $ \listener ->
|
||||
forever $ do
|
||||
acc@(_, host, port) <- accept listener
|
||||
putStrLn $ "Accepted connection from " ++ show (host, port)
|
||||
forkIO (echo acc)
|
||||
18
Task/Echo-server/Haskell/echo-server-2.hs
Normal file
18
Task/Echo-server/Haskell/echo-server-2.hs
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
global mlck, nCons
|
||||
|
||||
procedure main()
|
||||
mlck := mutex()
|
||||
nCons := 0
|
||||
while f := open(":12321","na") do {
|
||||
handle_client(f)
|
||||
critical mlck: if nCons <= 0 then close(f)
|
||||
}
|
||||
end
|
||||
|
||||
procedure handle_client(f)
|
||||
critical mlck: nCons +:= 1
|
||||
thread {
|
||||
select(f,1000) & repeat writes(f,reads(f))
|
||||
critical mlck: nCons -:= 1
|
||||
}
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue