Skip to content

Instantly share code, notes, and snippets.

@tanakh
Created September 14, 2012 06:22
Show Gist options
  • Select an option

  • Save tanakh/3720170 to your computer and use it in GitHub Desktop.

Select an option

Save tanakh/3720170 to your computer and use it in GitHub Desktop.
threadedにすると何故か遅い ref: http://qiita.com/items/6755af56e53ce184d327
{-# LANGUAGE OverloadedStrings #-}
import Control.Applicative
import Control.Concurrent (threadDelay)
import Control.Concurrent.Async (async, race_, wait)
import Control.Monad
import qualified Data.ByteString as S
import Data.ByteString.Char8 ()
import Data.Conduit
import Data.Conduit.Network
import Network
import System.Environment
import System.IO
main :: IO ()
main = withSocketsDo $ do
[tn, rn] <- map read <$> getArgs
server `race_` do
threadDelay 1000
mapM_ wait =<< replicateM tn (async $ client rn)
{-
server :: IO ()
server = runTCPServer (ServerSettings 12345 "*") $ \src sink -> src $$ sink
client :: Int -> IO ()
client n = runTCPClient (ClientSettings 12345 "localhost") $ \src sink ->
src $$ cond =$ sink
where
cond = replicateM_ n $ do
yield "hello"
void await
-}
server :: IO ()
server = do
ssock <- listenOn $ PortNumber 12345
forever $ do
(h, _, _) <- accept ssock
let go = do
bs <- S.hGetSome h 1024
when (not $ S.null bs) $ do
S.hPut h bs
hFlush h
go
void $ async go
client :: Int -> IO ()
client n = do
h <- connectTo "localhost" (PortNumber 12345)
replicateM_ n $ do
S.hPut h "hello"
hFlush h
_ <- S.hGetSome h 1024
return ()
hClose h
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment