on master help sign in
update and add networking examples
commitf1e2f03766562126de0d7370541016bdf106aabb authorHarendra Kumar <harendra.kumar@gmail.com> dateTue 28 May 2019, 10:13 (7 years ago) parent0eecbb5
3 files changed+35−13
examples/EchoServer.hs+15 examples/FileSinkServer.hs+3−5 examples/FromFileClient.hs+17−8
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)
2−import Data.Char (ord)
1+-- A concurrent TCP server that prints everything it receives to stdout.
32
43import Streamly
5−import qualified Streamly.FileSystem.File as FH
64import qualified Streamly.Fold as FL
75import qualified Streamly.Mem.Array as A
86import qualified Streamly.Network.Socket as NS
@@ -11,13 +9,13 @@ import qualified Streamly.Prelude as S
119
1210main :: IO ()
1311main = S.drain
14− $ parallely $ S.mapM (withSocket handle)
12+ $ parallely $ S.mapM (withSocket recv)
1513 $ serially $ fmap fst (NS.recvConnectionsOn 8090)
1614
1715 where
1816
1917 withSocket f sk = f sk >> NS.close sk
20− handle =
18+ recv =
2119 S.mapM_ print
2220 . FL.sessionsOf 3 FL.sum
2321 . 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+
26import qualified Streamly.Prelude as S
3−import Streamly.Network.Socket
4−import qualified Streamly.Mem.Array as A
5−import qualified Streamly.Fold as FL
7+import qualified Streamly.FileSystem.File as File
8+import qualified Streamly.Network.Socket as NS
69
710main :: IO ()
8−main = S.drain $ parallely
9− -- $ S.mapM (\sk -> writeArrays sk $ readArrays sk)
10− $ S.mapM (\sk -> S.mapM_ print $ FL.sessionsOf 10 FL.sum $ S.map A.length $ readArrays sk)
11− $ serially $ fmap fst (serveTCPOn 8090)
11+main = do
12+ openConnection >>= \sk ->
13+ S.drain $ NS.writeArrays sk $ File.readArrays stdin
14+
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