Haskell 向套接字客户端发送所有消息导致:`异常:Network.Socket.sendBuf:参数无效(传输端点未连接)`
Haskell sendAll message to socket client results in: `Exception: Network.Socket.sendBuf: invalid argument (Transport endpoint is not connected)`
从套接字服务器调用 Network.Socket.ByteString.sendAll
(hackage docs - sendall)(发送到套接字客户端)时,我 运行 遇到 *** Exception: Network.Socket.sendBuf: invalid argument (Transport endpoint is not connected)
错误。
我不确定为什么会出现此错误?好像我只能用一种方式发送数据?
我在使用 netcat 而不是 Haskell 客户端时也 运行 遇到了同样的错误:
echo 'test' | nc -N -U /tmp2/test2.soc
来自客户端的输出:
*Server Main> main
Hello, Haskell!2
"sent ping....."
服务器输出:
*Server> serv
"begin"
"Running daemon"
"begin2"
<socket: 13>
"Got message:"
"ping"
"Sending pong...."
*** Exception: Network.Socket.sendBuf: invalid argument (Transport endpoint is not connected)
完成项目:https://github.com/chrissound/UnixSocketPingPongHaskellTest
完整源代码:
客户:
{-# Language OverloadedStrings #-}
module Main where
import Network.Socket hiding (send)
import Network.Socket.ByteString as NBS
import Control.Concurrent
import Control.Monad
main :: IO ()
main = do
putStrLn "Hello, Haskell!2"
withSocketsDo $ do
soc <- socket AF_UNIX Stream 0
connect (soc) (SockAddrUnix "/tmp2/test2.soc")
forever $ do
send soc ("ping")
threadDelay $ 1 * 10^6
print "sent ping....."
threadDelay $ 1 * 10^6
msg <- NBS.recv soc 400000
print msg
print "got reply to ping...."
close soc
服务器:
{-# Language OverloadedStrings #-}
module Server where
import Network.Socket hiding (send)
import Network.Socket.ByteString as NBS
import Control.Concurrent
import Control.Monad
serv :: IO ()
serv = do
print "begin"
print "Running daemon"
soc <- socket AF_UNIX Stream 0
bind soc . SockAddrUnix $ "/tmp2/test2.soc"
listen soc maxListenQueue
accept soc >>= (\(x,y)-> do
print "begin2"
print x
print y
forever $ do
msg <- NBS.recv x 400000
print "Sending pong...."
NBS.sendAll soc "ppong"
print "alll done"
threadDelay $ 3 * 10^6
)
您正在侦听套接字上发送;您可能想改为在接受的套接字上发送。
listen soc maxListenQueue
accept soc >>= (\(x,y)-> do
...
NBS.sendAll soc "ppong" -- should be sendAll x "ppong"
从套接字服务器调用 Network.Socket.ByteString.sendAll
(hackage docs - sendall)(发送到套接字客户端)时,我 运行 遇到 *** Exception: Network.Socket.sendBuf: invalid argument (Transport endpoint is not connected)
错误。
我不确定为什么会出现此错误?好像我只能用一种方式发送数据?
我在使用 netcat 而不是 Haskell 客户端时也 运行 遇到了同样的错误:
echo 'test' | nc -N -U /tmp2/test2.soc
来自客户端的输出:
*Server Main> main
Hello, Haskell!2
"sent ping....."
服务器输出:
*Server> serv
"begin"
"Running daemon"
"begin2"
<socket: 13>
"Got message:"
"ping"
"Sending pong...."
*** Exception: Network.Socket.sendBuf: invalid argument (Transport endpoint is not connected)
完成项目:https://github.com/chrissound/UnixSocketPingPongHaskellTest
完整源代码:
客户:
{-# Language OverloadedStrings #-}
module Main where
import Network.Socket hiding (send)
import Network.Socket.ByteString as NBS
import Control.Concurrent
import Control.Monad
main :: IO ()
main = do
putStrLn "Hello, Haskell!2"
withSocketsDo $ do
soc <- socket AF_UNIX Stream 0
connect (soc) (SockAddrUnix "/tmp2/test2.soc")
forever $ do
send soc ("ping")
threadDelay $ 1 * 10^6
print "sent ping....."
threadDelay $ 1 * 10^6
msg <- NBS.recv soc 400000
print msg
print "got reply to ping...."
close soc
服务器:
{-# Language OverloadedStrings #-}
module Server where
import Network.Socket hiding (send)
import Network.Socket.ByteString as NBS
import Control.Concurrent
import Control.Monad
serv :: IO ()
serv = do
print "begin"
print "Running daemon"
soc <- socket AF_UNIX Stream 0
bind soc . SockAddrUnix $ "/tmp2/test2.soc"
listen soc maxListenQueue
accept soc >>= (\(x,y)-> do
print "begin2"
print x
print y
forever $ do
msg <- NBS.recv x 400000
print "Sending pong...."
NBS.sendAll soc "ppong"
print "alll done"
threadDelay $ 3 * 10^6
)
您正在侦听套接字上发送;您可能想改为在接受的套接字上发送。
listen soc maxListenQueue
accept soc >>= (\(x,y)-> do
...
NBS.sendAll soc "ppong" -- should be sendAll x "ppong"