欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

haskell实现多线程服务器实例代码

程序员文章站 2022-04-27 08:28:36
...
代码如下:


module Main where

import Network.Socket
import Control.Concurrent

main :: IO ()
main = do
sock bindSocket sock (SockAddrInet 4242 iNADDR_ANY)
listen sock 10240
mainLoop sock

mainLoop :: Socket -> IO ()
mainLoop sock = do
conn forkIO $ runConn conn
mainLoop sock

runConn :: (Socket, SockAddr) -> IO ()
runConn (sock, tcp) = do
sms peeraddr putStrLn sms
runConn (sock, tcp)