on master help sign in
update and add networking examples
f1e2f03Harendra Kumar
examples/EchoServer.hsnew file+15
@@ -0,0 +1,15 @@
1+-- A concurrent TCP server that echoes everything that it receives.
2+
3+import Streamly
4+import Streamly.Network.Socket
5+import qualified Streamly.Prelude as S
6+import qualified Network.Socket as NS
7+
8+main :: IO ()
9+main = S.drain
10+ $ parallely $ S.mapM (withSocket (\sk -> writeArrays sk $ readArrays sk))
11+ $ serially $ fmap fst (recvConnectionsOn 8090)
12+
13+ where
14+
15+ withSocket f sk = f sk >> NS.close sk
examples/FileSinkServer.hs+3−5
@@ -1,8 +1,6 @@
1−import System.IO (stdout)
1+-- A concurrent TCP server that prints everything it receives to stdout.
2−import Data.Char (ord)
3
2
4import Streamly
3import Streamly
5−import qualified Streamly.FileSystem.File as FH
6import qualified Streamly.Fold as FL
4import qualified Streamly.Fold as FL
7import qualified Streamly.Mem.Array as A
5import qualified Streamly.Mem.Array as A
8import qualified Streamly.Network.Socket as NS
6import qualified Streamly.Network.Socket as NS
@@ -11,13 +9,13 @@ import qualified Streamly.Prelude as S
11
9
12main :: IO ()
10main :: IO ()
13main = S.drain
11main = S.drain
14− $ parallely $ S.mapM (withSocket handle)
12+ $ parallely $ S.mapM (withSocket recv)
15 $ serially $ fmap fst (NS.recvConnectionsOn 8090)
13 $ serially $ fmap fst (NS.recvConnectionsOn 8090)
16
14
17 where
15 where
18
16
19 withSocket f sk = f sk >> NS.close sk
17 withSocket f sk = f sk >> NS.close sk
20− handle =
18+ recv =
21 S.mapM_ print
19 S.mapM_ print
22 . FL.sessionsOf 3 FL.sum
20 . FL.sessionsOf 3 FL.sum
23 . S.map A.length
21 . S.map A.length
examples/FromFileClient.hs+17−8
@@ -1,11 +1,20 @@
1−import Streamly
1+-- A TCP client that reads from stdin and sends to a server
2+
3+import Network.Socket
4+import System.IO (stdin)
5+
2import qualified Streamly.Prelude as S
6import qualified Streamly.Prelude as S
7+import qualified Streamly.FileSystem.File as File
3−import Streamly.Network.Socket
8+import qualified Streamly.Network.Socket as NS
4−import qualified Streamly.Mem.Array as A
5−import qualified Streamly.Fold as FL
6
9
7main :: IO ()
10main :: IO ()
8−main = S.drain $ parallely
11+main = do
12+ openConnection >>= \sk ->
9− -- $ S.mapM (\sk -> writeArrays sk $ readArrays sk)
13+ S.drain $ NS.writeArrays sk $ File.readArrays stdin
10− $ S.mapM (\sk -> S.mapM_ print $ FL.sessionsOf 10 FL.sum $ S.map A.length $ readArrays sk)
14+
11− $ serially $ fmap fst (serveTCPOn 8090)
15+ where
16+
17+ openConnection = do
18+ sock <- socket AF_INET Stream defaultProtocol
19+ connect sock $ SockAddrInet 8090 (tupleToHostAddress (127,0,0,1))
20+ return sock