From 158a6fd73fad1b152c00dcd7db249497e4076d3c Mon Sep 17 00:00:00 2001 From: Anton Gushcha Date: Sun, 19 Jul 2015 13:42:40 +0400 Subject: [PATCH 1/4] Update tutorial-client.hs withSocketsDo for Windows platform --- static/tutorial/tutorial-client.hs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/static/tutorial/tutorial-client.hs b/static/tutorial/tutorial-client.hs index b5677ae..a3acd5f 100644 --- a/static/tutorial/tutorial-client.hs +++ b/static/tutorial/tutorial-client.hs @@ -1,11 +1,12 @@ import Network.Transport import Network.Transport.TCP (createTransport, defaultTCPParameters) +import Network.Socket.Internal (withSocketsDo) import System.Environment import Control.Monad import Data.ByteString.Char8 main :: IO () -main = do +main = withSocketsDo $ do [host, port, serverAddr] <- getArgs Right transport <- createTransport host port defaultTCPParameters Right endpoint <- newEndPoint transport From 9aeb3da987585cd4b8694c4fba5a2c0feef2bf79 Mon Sep 17 00:00:00 2001 From: Anton Gushcha Date: Sun, 19 Jul 2015 13:44:51 +0400 Subject: [PATCH 2/4] Update tutorial-server.hs withSocketsDo for Windows platform --- static/tutorial/tutorial-server.hs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/static/tutorial/tutorial-server.hs b/static/tutorial/tutorial-server.hs index 1b7e065..68e318d 100644 --- a/static/tutorial/tutorial-server.hs +++ b/static/tutorial/tutorial-server.hs @@ -1,5 +1,6 @@ import Network.Transport import Network.Transport.TCP (createTransport, defaultTCPParameters) +import Network.Socket.Internal (withSocketsDo) import Control.Concurrent import Data.Map import Control.Exception @@ -44,7 +45,7 @@ p `onCtrlC` q = catchJust isUserInterrupt p (const $ q >> p `onCtrlC` q) isUserInterrupt _ = Nothing main :: IO () -main = do +main = withSocketsDo $ do [host, port] <- getArgs serverDone <- newEmptyMVar Right transport <- createTransport host port defaultTCPParameters From eb6d75d63132fc762a12db1619a29323fb91c286 Mon Sep 17 00:00:00 2001 From: Anton Gushcha Date: Sun, 19 Jul 2015 13:52:47 +0400 Subject: [PATCH 3/4] Update tutorial-NT2.md Add withSocketsDo for Windows platform, fromString is now pack --- tutorials/tutorial-NT2.md | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/tutorials/tutorial-NT2.md b/tutorials/tutorial-NT2.md index 5ae1ce8..5a47683 100644 --- a/tutorials/tutorial-NT2.md +++ b/tutorials/tutorial-NT2.md @@ -68,17 +68,18 @@ because it is simpler. We first need a bunch of imports: {% highlight haskell %} import Network.Transport -import Network.Transport.TCP (createTransport) +import Network.Transport.TCP (createTransport, defaultTCPParameters) +import Network.Socket.Internal (withSocketsDo) import System.Environment import Data.ByteString.Char8 import Control.Monad {% endhighlight %} -The client will consist of a single main function. +The client will consist of a single main function. [withSocketsDo](http://hackage.haskell.org/package/network-2.6.2.1/docs/Network-Socket-Internal.html#v:withSocketsDo) is needed for Windows platform. {% highlight haskell %} main :: IO () -main = do +main = withSocketsDo $ do {% endhighlight %} When we start the client we expect three command line arguments. @@ -157,14 +158,14 @@ That's it! Here is the entire client again: {% highlight haskell %} main :: IO () -main = do +main = withSocketsDo $ do [host, port, serverAddr] <- getArgs Right transport <- createTransport host port Right endpoint <- newEndPoint transport - let addr = EndPointAddress (fromString serverAddr) + let addr = EndPointAddress (pack serverAddr) Right conn <- connect endpoint addr ReliableOrdered defaultConnectHints - send conn [fromString "Hello world"] + send conn [pack "Hello world"] close conn replicateM_ 3 $ receive endpoint >>= print @@ -180,7 +181,8 @@ start with a bunch of imports: {% highlight haskell %} import Network.Transport -import Network.Transport.TCP (createTransport) +import Network.Transport.TCP (createTransport, defaultTCPParameters) +import Network.Socket.Internal (withSocketsDo) import Control.Concurrent import Data.Map import Control.Exception @@ -191,10 +193,10 @@ We will write the main function first: {% highlight haskell %} main :: IO () -main = do +main = withSocketsDo $ do [host, port] <- getArgs serverDone <- newEmptyMVar - Right transport <- createTransport host port + Right transport <- createTransport host port defaultTCPParameters Right endpoint <- newEndPoint transport forkIO $ echoServer endpoint serverDone putStrLn $ "Echo server started at " ++ show (address endpoint) From e516238441a40094b072dd1609f2a74d328ba6cd Mon Sep 17 00:00:00 2001 From: Anton Gushcha Date: Mon, 20 Jul 2015 12:27:32 +0400 Subject: [PATCH 4/4] Update tutorial-NT2.md withSocketsDo may be needed, emphasize the point --- tutorials/tutorial-NT2.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tutorials/tutorial-NT2.md b/tutorials/tutorial-NT2.md index 5a47683..fabcd40 100644 --- a/tutorials/tutorial-NT2.md +++ b/tutorials/tutorial-NT2.md @@ -75,7 +75,7 @@ import Data.ByteString.Char8 import Control.Monad {% endhighlight %} -The client will consist of a single main function. [withSocketsDo](http://hackage.haskell.org/package/network-2.6.2.1/docs/Network-Socket-Internal.html#v:withSocketsDo) is needed for Windows platform. +The client will consist of a single main function. [withSocketsDo](http://hackage.haskell.org/package/network-2.6.2.1/docs/Network-Socket-Internal.html#v:withSocketsDo) may be needed for Windows platform with old versions of network library. For compatibility with older versions on Windows, it is good practice to always call withSocketsDo (it's very cheap). {% highlight haskell %} main :: IO ()