首页 > 编程 > Python > 正文

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

2019-11-25 18:37:02
字体:
来源:转载
供稿:网友

复制代码 代码如下:

module Main where

import Network.Socket
import Control.Concurrent

main :: IO ()
main = do
    sock <- socket AF_INET Stream 0
    bindSocket sock (SockAddrInet 4242 iNADDR_ANY)
    listen sock 10240
    mainLoop sock

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

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

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表