Skip to content

Network.Transport tutorial: Fixes for code mistakes #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion static/tutorial/tutorial-client.hs
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 2 additions & 1 deletion static/tutorial/tutorial-server.hs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down
20 changes: 11 additions & 9 deletions tutorials/tutorial-NT2.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) 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 ()
main = do
main = withSocketsDo $ do
{% endhighlight %}

When we start the client we expect three command line arguments.
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand Down