Skip to content

Commit ab2f241

Browse files
authored
Merge pull request #18 from NCrashed/master
Network.Transport tutorial: Fixes for code mistakes
2 parents 7e9e682 + e516238 commit ab2f241

File tree

3 files changed

+15
-11
lines changed

3 files changed

+15
-11
lines changed

Diff for: static/tutorial/tutorial-client.hs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import Network.Transport
22
import Network.Transport.TCP (createTransport, defaultTCPParameters)
3+
import Network.Socket.Internal (withSocketsDo)
34
import System.Environment
45
import Control.Monad
56
import Data.ByteString.Char8
67

78
main :: IO ()
8-
main = do
9+
main = withSocketsDo $ do
910
[host, port, serverAddr] <- getArgs
1011
Right transport <- createTransport host port defaultTCPParameters
1112
Right endpoint <- newEndPoint transport

Diff for: static/tutorial/tutorial-server.hs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Network.Transport
22
import Network.Transport.TCP (createTransport, defaultTCPParameters)
3+
import Network.Socket.Internal (withSocketsDo)
34
import Control.Concurrent
45
import Data.Map
56
import Control.Exception
@@ -44,7 +45,7 @@ p `onCtrlC` q = catchJust isUserInterrupt p (const $ q >> p `onCtrlC` q)
4445
isUserInterrupt _ = Nothing
4546

4647
main :: IO ()
47-
main = do
48+
main = withSocketsDo $ do
4849
[host, port] <- getArgs
4950
serverDone <- newEmptyMVar
5051
Right transport <- createTransport host port defaultTCPParameters

Diff for: tutorials/tutorial-NT2.md

+11-9
Original file line numberDiff line numberDiff line change
@@ -68,17 +68,18 @@ because it is simpler. We first need a bunch of imports:
6868

6969
{% highlight haskell %}
7070
import Network.Transport
71-
import Network.Transport.TCP (createTransport)
71+
import Network.Transport.TCP (createTransport, defaultTCPParameters)
72+
import Network.Socket.Internal (withSocketsDo)
7273
import System.Environment
7374
import Data.ByteString.Char8
7475
import Control.Monad
7576
{% endhighlight %}
7677

77-
The client will consist of a single main function.
78+
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).
7879

7980
{% highlight haskell %}
8081
main :: IO ()
81-
main = do
82+
main = withSocketsDo $ do
8283
{% endhighlight %}
8384

8485
When we start the client we expect three command line arguments.
@@ -157,14 +158,14 @@ That's it! Here is the entire client again:
157158

158159
{% highlight haskell %}
159160
main :: IO ()
160-
main = do
161+
main = withSocketsDo $ do
161162
[host, port, serverAddr] <- getArgs
162163
Right transport <- createTransport host port
163164
Right endpoint <- newEndPoint transport
164165

165-
let addr = EndPointAddress (fromString serverAddr)
166+
let addr = EndPointAddress (pack serverAddr)
166167
Right conn <- connect endpoint addr ReliableOrdered defaultConnectHints
167-
send conn [fromString "Hello world"]
168+
send conn [pack "Hello world"]
168169
close conn
169170

170171
replicateM_ 3 $ receive endpoint >>= print
@@ -180,7 +181,8 @@ start with a bunch of imports:
180181

181182
{% highlight haskell %}
182183
import Network.Transport
183-
import Network.Transport.TCP (createTransport)
184+
import Network.Transport.TCP (createTransport, defaultTCPParameters)
185+
import Network.Socket.Internal (withSocketsDo)
184186
import Control.Concurrent
185187
import Data.Map
186188
import Control.Exception
@@ -191,10 +193,10 @@ We will write the main function first:
191193

192194
{% highlight haskell %}
193195
main :: IO ()
194-
main = do
196+
main = withSocketsDo $ do
195197
[host, port] <- getArgs
196198
serverDone <- newEmptyMVar
197-
Right transport <- createTransport host port
199+
Right transport <- createTransport host port defaultTCPParameters
198200
Right endpoint <- newEndPoint transport
199201
forkIO $ echoServer endpoint serverDone
200202
putStrLn $ "Echo server started at " ++ show (address endpoint)

0 commit comments

Comments
 (0)