} } }

    [摘录]haskell实现办事器端,ruby实现客户端

    添加时间:2013-8-8 点击量:

    server.hs



    import Network.Socket
    

    main :: IO ()
    main
    = do
    -- create socket
    sock
    <- socket AF_INET Stream 0
    -- make socket immediately reusable - eases debugging.
    setSocketOption sock ReuseAddr
    1
    -- listen on TCP port 4242
    bindSocket sock (SockAddrInet
    4242 iNADDR_ANY)
    -- allow a maximum of 2 outstanding connections
    listen sock
    2
    mainLoop sock

    mainLoop :: Socket
    -> IO ()
    mainLoop sock
    = do
    -- accept one connection and handle it
    conn
    <- accept sock
    runConn conn
    mainLoop sock

    runConn :: (Socket, SockAddr)
    -> IO ()
    runConn (sock, tcp)
    = do
    sms
    <-recv sock 1024
    putStr sms
    send sock
    I recevied the message!\n
    runConn (sock, tcp)


    client.rb



    require socket
    
    s
    = TCPSocket.new localhost,4242
    loop do
    sms
    =gets.chomp
    s.puts sms
    puts s.gets
    end


    彼此相爱,却不要让爱成了束缚:不如让它成为涌动的大海,两岸乃是你们的灵魂。互斟满杯,却不要同饮一杯。相赠面包,却不要共食一个。一起歌舞欢喜,却依然各自独立,相互交心,却不是让对方收藏。因为唯有生命之手,方能收容你们的心。站在一起却不要过于靠近。—— 纪伯伦《先知》
    分享到: