Skip to content

Instantly share code, notes, and snippets.

@nakal
Last active Aug 29, 2015
Embed
What would you like to do?
Threaded TCP-Server in Haskell (IPv4/IPv6)
import Network.Socket as N
import Control.Concurrent
serverPort :: PortNumber
serverPort = 8080
main = do
sock <- N.socket N.AF_INET6 N.Stream N.defaultProtocol
N.setSocketOption sock N.ReuseAddr 1
N.setSocketOption sock N.ReusePort 1
N.bind sock addr
N.listen sock 5
acceptLoop sock
where addr = N.SockAddrInet6 serverPort 0 N.iN6ADDR_ANY 0
acceptLoop :: Socket -> IO()
acceptLoop sock = do
(csock, addr) <- N.accept sock
forkIO $ doConnection csock addr
acceptLoop sock
doConnection :: Socket -> SockAddr -> IO()
doConnection socket addr = do
sent <- N.send socket "Hello world!\n"
N.shutdown socket N.ShutdownBoth
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment