Sockets again

Keean Schupke k.schupke@imperial.ac.uk
Tue, 29 Apr 2003 17:43:30 +0100


Hi,
    The problem is not with the haskell, but with telnet, which uses 
line buffered output itself.

Try the following:

module Main(main) where

import IO
import Network

main = do
    socket <- listenOn (PortNumber 15151)
    (handle,hostName,_) <- accept socket
    hSetBuffering handle (BlockBuffering (Just 4096))
    putStr ("Accepted: "++hostName++"\n")
    c <- hGetLine handle
    putStrLn (show c)

telnet to this, type a line and hit return ... should print the line.

    Regards,
    Keean Schupke

George Russell wrote:

> The attached short program (compile with "ghc VServer.hs -o v -package 
> net")
> is supposed to set up a server on port 15151, wait for a connection, read
> the first character from the connection, and print it out.  Unfortunately
> if I test it, by running it, and starting up "telnet [machine] 15151"
> somewhere else, and then type some random text, EG "foo[RETURN]", it does
> not work.  It looks as if the problem is that VServer.hs issues the
> command
>    hSetBuffering handle (BlockBuffering (Just 4096))
> on the connection, because when I change it to
>    hSetBuffering handle NoBuffering
> the program works.
>
> However this is not what I want to do!!  Because setting NoBuffering 
> on the
> handle is going to mean that when the Server *outputs* something, it will
> potentially be done very expensively character by character.  How do I
> get block buffering on the Server's output, but not have input to the
> server held up?
>
> The same thing seems to happen with client Handles by the way.
>
>------------------------------------------------------------------------
>
>-- Test program for server using GHC Socket library.
>module Main(main) where
>
>import IO
>import Network
>
>main =
>   do
>      socket <- listenOn (PortNumber 15151)
>      (handle,hostName,_) <- accept socket
>      hSetBuffering handle (BlockBuffering (Just 4096))
>      putStr ("Accepted: "++hostName++"\n")
>      c <- hGetChar handle
>      putStrLn (show c)
>
>  
>