Re: [Haskell-cafe] UDP
Andrew Coppin ha scritto: [...] Yeah, I just assumed that the bind step was only necessary for connection-oriented protocols. (Interestingly enough, the matching send program doesn't bind at all, yet seems to work fine...) For a client (that is, when you call connect), the kernel chooses the source IP address once the socket is connected. Of course, for a server this is not feasible, since the address *must* be know to external programs, if they want to connect to the server. For more details, I really suggest to read a good book like UNIX Network Programming, by W. Richard Stevens. Regards Manlio Perillo ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] UDP
John Van Enk wrote: Try something like this: module Main where import Network.Socket main = withSocketsDo $ do -- Make a UDP socket s - socket AF_INET Datagram defaultProtocol -- We want to listen on all interfaces (0.0.0.0) bindAddr - inet_addr 0.0.0.0 -- Bind to 0.0.0.0:3 http://0.0.0.0:3 bindSocket s (SockAddrInet 3 bindAddr) -- Read a message of max length 1000 from some one (msg,len,from) - recvFrom s 1000 putStrLn $ Got the following message from ++ (show from) putStrLn msg Does this help? As Stephan said, you missed the bind step. That works great, thanks. Yeah, I just assumed that the bind step was only necessary for connection-oriented protocols. (Interestingly enough, the matching send program doesn't bind at all, yet seems to work fine...) ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] UDP
2009/2/1 Andrew Coppin andrewcop...@btinternet.com: Yeah, I just assumed that the bind step was only necessary for connection-oriented protocols. (Interestingly enough, the matching send program doesn't bind at all, yet seems to work fine...) socket() system call creates a socket (a descriptor) that you can identify. bind() creates an identity for the socket so that applications outside can refer to it (using ip:port); it also enables the kernel to pass the received data to your application. sendto() doesn't require that identity. -- Vimal ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] UDP
Andrew Coppin wrote: I'm trying to write a simple program that involves UDP. I was hoping something like this would work: [...] How about using bindSocket? At least that's the main difference between your code snippet and our (UDP-using) barracuda project :) main2 = do s - socket AF_INET Datagram defaultProtocol bindSocket s ... putStrLn Waiting... x - recv s 100 putStrLn x [...] //Stephan -- Früher hieß es ja: Ich denke, also bin ich. Heute weiß man: Es geht auch so. - Dieter Nuhr ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] UDP
The network library is no more than an FFI library to a Berkeley socket interface and as such it implicitly expects you to know sockets already (eg. from programming in C). One advantage here is reading man pages actually helps (unlike with most Haskell coding) and you can also make equivalent C programs to test things out. In the long term we should design and build a more functional network library. Thomas On Sat, Jan 31, 2009 at 9:19 PM, Stephan Friedrichs deduktionstheo...@web.de wrote: Andrew Coppin wrote: I'm trying to write a simple program that involves UDP. I was hoping something like this would work: [...] How about using bindSocket? At least that's the main difference between your code snippet and our (UDP-using) barracuda project :) main2 = do s - socket AF_INET Datagram defaultProtocol bindSocket s ... putStrLn Waiting... x - recv s 100 putStrLn x [...] //Stephan -- Früher hieß es ja: Ich denke, also bin ich. Heute weiß man: Es geht auch so. - Dieter Nuhr ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] UDP
Try something like this: module Main where import Network.Socket main = withSocketsDo $ do -- Make a UDP socket s - socket AF_INET Datagram defaultProtocol -- We want to listen on all interfaces (0.0.0.0) bindAddr - inet_addr 0.0.0.0 -- Bind to 0.0.0.0:3 bindSocket s (SockAddrInet 3 bindAddr) -- Read a message of max length 1000 from some one (msg,len,from) - recvFrom s 1000 putStrLn $ Got the following message from ++ (show from) putStrLn msg Does this help? As Stephan said, you missed the bind step. /jve On Sun, Jan 25, 2009 at 11:22 AM, Andrew Coppin andrewcop...@btinternet.com wrote: I'm trying to write a simple program that involves UDP. I was hoping something like this would work: module Main where import Network.Socket main = withSocketsDo main2 main2 = do s - socket AF_INET Datagram defaultProtocol putStrLn Waiting... x - recv s 100 putStrLn x Unfortunately, that doesn't work at all. It immediately throws an exception (unknown error). But then, the whole module seems to be completely undocumented. I managed to find a tiny amount of info online about the underlying C API, but I still don't get how the Haskell interface is supposed to be used. Any hints? ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] UDP
Thomas DuBuisson wrote: The network library is no more than an FFI library to a Berkeley socket interface and as such it implicitly expects you to know sockets already (eg. from programming in C). One advantage here is reading man pages actually helps (unlike with most Haskell coding) and you can also make equivalent C programs to test things out. Yes, that's kind of the problem; I don't know how to do this at the C level, and I can't seem to Google it. :-} Ah well, I'll ask around. Somebody must know. ;-) In the long term we should design and build a more functional network library. Well, I guess having a library that gives you low-level access means that anybody who wants to have a go can easily build something nicer on top of that. (As opposed to, say, file I/O where there is only the high-level interface, so if you want to do something that isn't implemented... you can't.) ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] UDP
On Sat, Jan 31, 2009 at 9:36 PM, Andrew Coppin andrewcop...@btinternet.com wrote: Thomas DuBuisson wrote: The network library is no more than an FFI library to a Berkeley socket interface and as such it implicitly expects you to know sockets already (eg. from programming in C). One advantage here is reading man pages actually helps (unlike with most Haskell coding) and you can also make equivalent C programs to test things out. Yes, that's kind of the problem; I don't know how to do this at the C level, and I can't seem to Google it. :-} Ah well, I'll ask around. Somebody must know. ;-) Ahh, then see Beej's guide [1] if you wish to learn at the C level. On another note: when making your Haskell app if you are at all performance concerned then you should use network-bytestring [2]. [1] http://beej.us/guide/bgnet/ [2] http://hackage.haskell.org/cgi-bin/hackage-scripts/package/network-bytestring ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] UDP client/server
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
Re: [Haskell-cafe] UDP client/server
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 [EMAIL PROTECTED] wrote: 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 ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] UDP client/server
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 ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] UDP client/server
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 ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] UDP client/server
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 [EMAIL PROTECTED] wrote: 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 ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] UDP client/server
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 0x0107f) return () Thanks everyone for your help. -John On 1/12/07, John Ky [EMAIL PROTECTED] wrote: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 ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe