Take the 2-minute tour ×

I intend to build on this code, found here However, I notice I can telnet to this server on the local host. Can't from another computer. I did a quick nmap scan, which reported that nothing was listening on the port I had selected. For purposes of troubleshooting, I had shut down my firewall, so I've ruled that out as a possible problem.

Clues from haskell windows programmers would be appreciated.

share|improve this question
what nmap flags you used? simpler using netstat -a -p tcp –  Eric Fortis Sep 25 '11 at 1:59
add comment

3 Answers

I usually go with

netstat -an | grep LISTEN

If you see the port listed, something is listening. I can't remember offhand what the lsof command is for sockets and Google isn't giving up the goods.

share|improve this answer
it's lsof -i:port –  lunixbochs Sep 25 '11 at 3:15
add comment

It seems that the socket got bind to localhost (, thats why you are not able to connect it from other machine and it only connect from local machine. Try to use Bind API to first create the socket and then bind the socket to "Any address" which binds the socket to every interface available on local machine.

share|improve this answer
This makes the most sense. I'll try this later today. –  Michael Litchard Sep 26 '11 at 18:17
I'm improving the code based on a reddit post concerning this tcp server example. I'll post what I did when it's ready for critique. –  Michael Litchard Sep 26 '11 at 23:43
add comment
up vote 1 down vote accepted

This is for future new haskellers. I based my code on this example. I made improvements based on this reddit thread, and suggestions made above. The import statements are still sloppy, but fixing them is left as the proverbial "exercise for the reader". I invite any additional suggestions leading to improvements.

import Network.Socket
import Control.Monad
import Network
import System.Environment (getArgs)
import System.IO
import Control.Concurrent (forkIO)

main :: IO ()
main = withSocketsDo $ do
   [portStr] <- getArgs
   sock <- socket AF_INET Stream defaultProtocol 
   let port = fromIntegral (read portStr :: Int)
       socketAddress = SockAddrInet port 0000 
   bindSocket sock socketAddress
   listen sock 1
   putStrLn $ "Listening on " ++ (show port)
   sockHandler sock

sockHandler :: Socket -> IO ()
sockHandler sock' = forever $ do
    (sock, _) <- Network.Socket.accept sock'
    handle <- socketToHandle sock ReadWriteMode
    hSetBuffering handle NoBuffering
    forkIO $ commandProcessor handle

commandProcessor :: Handle -> IO ()
commandProcessor  handle = forever $ do
    line <- hGetLine handle
    let (cmd:arg) = words line
    case cmd of
        "echo" -> echoCommand handle arg 
        "add" -> addCommand handle arg 
        _ -> do hPutStrLn handle "Unknown command"

echoCommand :: Handle -> [String] -> IO ()
echoCommand handle arg = do
    hPutStrLn handle (unwords arg)

addCommand :: Handle -> [String] -> IO ()  
addCommand handle [x,y] = do
    hPutStrLn handle $ show $ read x + read y
addCommand handle _ = do
    hPutStrLn handle "usage: add Int Int"
share|improve this answer
add comment

Your Answer


