我试图根据haskell.org提供的示例在Haskell中创建TCP
服务器。当我使用ghc run Main.hs
从终端运行代码时,我得到:
< br />
[1 of 1] Compiling Main ( Main.hs, Main.o )
Linking Main.exe ...
关闭之后。
代码
module Main where
import Network.Socket
import Network.Socket.ByteString
import qualified Data.ByteString.Char8 as C
main::IO()
main=do
sock<-socket AF_INET Stream 0
bind sock (SockAddrInet 8100 iNADDR_ANY)
listen sock 2
mainloop sock
mainloop::Socket->IO()
mainloop sock=do
conn<-accept sock
runConn conn
mainloop sock
runConn::(Socket,SockAddr)->IO()
runConn (sock,_)=do
Network.Socket.ByteString.send sock (C.pack "hello")
close sock
我首先假设服务器可能以某种方式在背景中运行,所以我使用Simple Web Socket Client
-浏览器扩展程序和nada对其进行了测试。
经过测试的网址
ws://localhost:8100
答案 0 :(得分:3)
您需要在编译二进制文件后执行它们:
在Linux上为./Main
,在Windows上为Main.exe
。
然后,您可以使用telnet localhost 8100
telnet localhost 8100
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
helloConnection closed by foreign host.