27
我想用Haskell和節儉做一個簡單的乒乓球。但是,它只會重複一次,然後卡住了。我認爲問題出在Thrift的正確使用中,而不是在Haskell中。可能有些東西沒有正確刷新。有沒有人有過Thrift的經驗,可以幫助我對如何解決這個問題進行有根據的猜測?與Haskell和節儉的乒乓球卡住
服務器:
echorequest :: TXT
echorequest = TXT {
f_TXT_anytxt = Just "Ping"
}
echoreply :: TXT
echoreply = TXT {
f_TXT_anytxt = Just "Pong"
}
serverFunc :: a -> (BinaryProtocol Handle, BinaryProtocol Handle)
-> IO Bool
serverFunc a (h1,h2) = do
let t1 = getTransport h1
dat <- read_TXT h1
-- the following two lines are only for debugging
putStrLn "Recieved data:"
print dat
write_TXT h1 echoreply
tFlush t1
-- the following line is for debugging
putStrLn "Data written"
return False
main :: IO()
main = do
runBasicServer() serverFunc 4390
putStrLn "Server stopped"
客戶:
main :: IO()
main = do
h <- connectTo "127.0.0.1" $ PortNumber 4390
let proto = BinaryProtocol h
putStrLn "Client started"
let tryOnePing c i = do
write_TXT proto echorequest
putStrLn "ping sent"
tFlush h
w <- read_TXT proto
putStrLn "pong received"
return $ if w == echoreply then c+1 else c
c <- foldM tryOnePing 0 [0 .. 1000]
tClose h
print (c, 10000 - c)
,我不認爲你的代碼片段足以讓任何人來幫助你。 read_TXT是如何實現的?如果它使用'hGetContents'之類的東西,那麼這就是你的問題 - 打印不知道什麼時候所有的內容都被接收到了,並且會阻塞到EOF。這可能是關閉連接並打開新連接的原因。 –
@ ThomasM.DuBuisson這是節儉閱讀功能。在這種情況下讀取一個結構。 –