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

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

程序员文章站 2022-05-12 13:35:32
复制代码 代码如下:module main where import network.socketimport control.concurrent main :: i...

复制代码 代码如下:

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)