
Hello, Does anyone know where I can find a simple UDP client/server written in Haskell? Something along the lines of an echo server would do. Thanks -John

On Thu, 11 Jan 2007, John Ky wrote:
Does anyone know where I can find a simple UDP client/server written in Haskell?
There is some support as part of a SuperCollider wrapper: http://www.slavepianos.org/rd/sw/sw-69/Sound/OpenSoundControl/UDP.hs

I think he meant something more along the lines of (or exactly) this, but in
Haskell
http://java.sun.com/docs/books/tutorial/networking/sockets/clientServer.html
I for one would also be interested in reading a tutorial like this using the
ghc libs
-Dan
On 1/11/07, Henning Thielemann
On Thu, 11 Jan 2007, John Ky wrote:
Does anyone know where I can find a simple UDP client/server written in Haskell?
There is some support as part of a SuperCollider wrapper: http://www.slavepianos.org/rd/sw/sw-69/Sound/OpenSoundControl/UDP.hs _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Hi John, On Jan 11, 2007, at 1:58 AM, John Ky wrote:
Hello,
Does anyone know where I can find a simple UDP client/server written in Haskell?
Something along the lines of an echo server would do.
Thanks
-John
Try: -- -- UDPEchoServer.hs: Exactly what the name says, a datagram echo server. -- module Main (main) where import Network.Socket import System.Posix.Directory import System.Posix.Files import System.Posix.IO import System.Posix.Process import System.Exit echoPort = 9900 maxline = 1500 -- -- The daemon infrastructure -- main :: IO () main = do pid <- forkProcess child exitImmediately ExitSuccess child :: IO () child = do -- Set up the working directory, mask and standard i/o -- for a daemon process (these will be inherited by -- the forked process): changeWorkingDirectory "/" setFileCreationMask 0 mapM_ closeFd [stdInput, stdOutput, stdError] nullFd <- openFd "/dev/null" ReadWrite Nothing defaultFileFlags mapM_ (dupTo nullFd) [stdInput, stdOutput, stdError] closeFd nullFd createSession -- This child becomes a process and session -- group leader. This prevents the child of -- this process (the daemon) from -- ever getting a controlling terminal. pid' <- forkProcess echoserver exitImmediately ExitSuccess -- -- The echo server daemon -- echoserver :: IO () echoserver = do withSocketsDo $ do sock <- socket AF_INET Datagram 0 bindSocket sock (SockAddrInet echoPort iNADDR_ANY) socketEcho sock socketEcho :: Socket -> IO () socketEcho sock = do (mesg, recv_count, client) <- recvFrom sock maxline send_count <- sendTo sock mesg client socketEcho sock ------------------------------------------------------------------------ ------------------------------- On my OS X/ppc 10.4.8 system, the above builds with ghc 6.6 and if I open one terminal with gregory-wrights-powerbook-g4-17> nc -u 127.0.0.1 9900 and another with gregory-wrights-powerbook-g4-17> nc -ul -p 9900 127.0.0.1 whatever I type into the first terminal appears on the second. You may have to consult your documentation for the options to your version of nc (or netcat, if you use that instead). I was also able to see that the server returned packets using hping3. Needless to say, the above is just an example, and is by no means bulletproof. I think I adapted it from something I found on the old wiki and updated it to work with the current libraries. Best Wishes, Greg

Hi John, On Jan 11, 2007, at 10:35 AM, Gregory Wright wrote:
Hi John,
On Jan 11, 2007, at 1:58 AM, John Ky wrote:
Hello,
Does anyone know where I can find a simple UDP client/server written in Haskell?
Something along the lines of an echo server would do.
Thanks
-John
Try:
<snip> For testing, you need only use gregory-wrights-powerbook-g4-17> nc -ul -p 9900 127.0.0.1 and whatever you type should be echoed. My original description of how to test:
On my OS X/ppc 10.4.8 system, the above builds with ghc 6.6 and if I open one terminal with
gregory-wrights-powerbook-g4-17> nc -u 127.0.0.1 9900
and another with
gregory-wrights-powerbook-g4-17> nc -ul -p 9900 127.0.0.1
whatever I type into the first terminal appears on the second. You may have to consult your documentation for the options to your version of nc (or netcat, if you use that instead).
is wrong. (It will copy from one terminal to the other when the daemon is not present.) Best, Greg

Hi,
What's wrong with my UDP client?
echoClient :: IO ()
echoClient = withSocketsDo $ do
putStrLn "[a]"
sock <- socket AF_INET Datagram 0
putStrLn "[b]"
connect sock (SockAddrInet 9900 iNADDR_ANY)
putStrLn "[c]"
n <- send sock "hi"
putStrLn "[d]"
return ()
I get:
*Main> echoClient
[a]
[b]
*** Exception: connect: failed (Cannot assign requested address
(WSAEADDRNOTAVAI
L))
Thanks
-John
On 1/12/07, Gregory Wright
Hi John,
On Jan 11, 2007, at 10:35 AM, Gregory Wright wrote:
Hi John,
On Jan 11, 2007, at 1:58 AM, John Ky wrote:
Hello,
Does anyone know where I can find a simple UDP client/server written in Haskell?
Something along the lines of an echo server would do.
Thanks
-John
Try:
<snip>
For testing, you need only use
gregory-wrights-powerbook-g4-17> nc -ul -p 9900 127.0.0.1
and whatever you type should be echoed. My original description of how to test:
On my OS X/ppc 10.4.8 system, the above builds with ghc 6.6 and if I open one terminal with
gregory-wrights-powerbook-g4-17> nc -u 127.0.0.1 9900
and another with
gregory-wrights-powerbook-g4-17> nc -ul -p 9900 127.0.0.1
whatever I type into the first terminal appears on the second. You may have to consult your documentation for the options to your version of nc (or netcat, if you use that instead).
is wrong. (It will copy from one terminal to the other when the daemon is not present.)
Best, Greg

Nevermind,
I just got the client to work:
echoClient :: IO ()
echoClient = withSocketsDo $ do
sock <- socket AF_INET Datagram 0
n <- sendTo sock "hi" (SockAddrInet echoPort 0x01000007f)
return ()
Thanks everyone for your help.
-John
On 1/12/07, John Ky
participants (4)
-
Dan Mead
-
Gregory Wright
-
Henning Thielemann
-
John Ky