This starts a TCP server on localhost, sends "Hello world!" to connecting peers and closes the connection immediately.

{-# LANGUAGE OverloadedStrings #-} module Main where import System.Socket import System.Socket.Family.INET (inaddrLOOPBACK) import Data.Monoid import Data.ByteString import Control.Monad import Control.Concurrent import Control.Exception main :: IO () main = do s <- socket :: IO (Socket INET STREAM TCP) setSockOpt s (SO_REUSEADDR True) bind s (SockAddrIn 8080 inaddrLOOPBACK) listen s 5 forever $ do (peer,addr) <- accept s forkIO $ do sendAll peer "Hello world!" mempty `finally` close peer

This downloads the [Haskell website](http://www.haskell.org) and shows how to handle exceptions. Note the use of IPv4-mapped INET6 addresses: This will work even if you don't have IPv6 connectivity yet and is the preferred method when writing new applications.

{-# LANGUAGE OverloadedStrings #-} module Main where import Data.Monoid import Data.ByteString.Lazy as B import System.Socket main :: IO () main = do withConnectedSocket "www.haskell.org" "80" (aiALL `mappend` aiV4MAPPED) $ \sock-> do let _ = sock :: Socket INET6 STREAM TCP sendAll sock "GET / HTTP/1.0\r

Host: www.haskell.org\r

\r

" mempty x <- recvAll sock (1024*1024*1024) mempty B.putStr x