Skip to main content
Commonmark migration
Source Link

#Haskell, 326 bytes

Haskell, 326 bytes

import Data.Bits import Network.Socket import System.IO f n=withSocketsDo$do s<-socket AF_INET Stream defaultProtocol bind s$SockAddrInet n iNADDR_ANY listen s 1 g s g s=do (z,SockAddrInet _ a)<-accept s h<-socketToHandle z WriteMode hPutStrLn h$show$b a hClose h g s b 0=0 b x=x.&.0xFF+b(x`shiftR`8) 

Sadly I had to use Network.Socket to get access to the remote IP address as an integer rather than a string. It would have saved dozens of characters if I could just do s <- listenOn (PortNumber n), rather than having to explicitly call socket, bind and listen individually. But, sadly, Network.accept gives me a host string, not an IP address integer, so I had to resort to Network.Socket.accept and friends.

The function f takes a port number as argument, and creates a server socket (s) listening on that port. It then calls the function g with the server socket. g loops forever, accepting connections. The function b takes an actual IPv4 address and computes the sum of its digits.

I'm sure somebody somewhere can do this better than me. I wanted to show off just how damned easy socket stuff is in Haskell... but then failed miserably, because I need access to the IP address, which isn't usually easy to get.

#Haskell, 326 bytes

import Data.Bits import Network.Socket import System.IO f n=withSocketsDo$do s<-socket AF_INET Stream defaultProtocol bind s$SockAddrInet n iNADDR_ANY listen s 1 g s g s=do (z,SockAddrInet _ a)<-accept s h<-socketToHandle z WriteMode hPutStrLn h$show$b a hClose h g s b 0=0 b x=x.&.0xFF+b(x`shiftR`8) 

Sadly I had to use Network.Socket to get access to the remote IP address as an integer rather than a string. It would have saved dozens of characters if I could just do s <- listenOn (PortNumber n), rather than having to explicitly call socket, bind and listen individually. But, sadly, Network.accept gives me a host string, not an IP address integer, so I had to resort to Network.Socket.accept and friends.

The function f takes a port number as argument, and creates a server socket (s) listening on that port. It then calls the function g with the server socket. g loops forever, accepting connections. The function b takes an actual IPv4 address and computes the sum of its digits.

I'm sure somebody somewhere can do this better than me. I wanted to show off just how damned easy socket stuff is in Haskell... but then failed miserably, because I need access to the IP address, which isn't usually easy to get.

Haskell, 326 bytes

import Data.Bits import Network.Socket import System.IO f n=withSocketsDo$do s<-socket AF_INET Stream defaultProtocol bind s$SockAddrInet n iNADDR_ANY listen s 1 g s g s=do (z,SockAddrInet _ a)<-accept s h<-socketToHandle z WriteMode hPutStrLn h$show$b a hClose h g s b 0=0 b x=x.&.0xFF+b(x`shiftR`8) 

Sadly I had to use Network.Socket to get access to the remote IP address as an integer rather than a string. It would have saved dozens of characters if I could just do s <- listenOn (PortNumber n), rather than having to explicitly call socket, bind and listen individually. But, sadly, Network.accept gives me a host string, not an IP address integer, so I had to resort to Network.Socket.accept and friends.

The function f takes a port number as argument, and creates a server socket (s) listening on that port. It then calls the function g with the server socket. g loops forever, accepting connections. The function b takes an actual IPv4 address and computes the sum of its digits.

I'm sure somebody somewhere can do this better than me. I wanted to show off just how damned easy socket stuff is in Haskell... but then failed miserably, because I need access to the IP address, which isn't usually easy to get.

Source Link

#Haskell, 326 bytes

import Data.Bits import Network.Socket import System.IO f n=withSocketsDo$do s<-socket AF_INET Stream defaultProtocol bind s$SockAddrInet n iNADDR_ANY listen s 1 g s g s=do (z,SockAddrInet _ a)<-accept s h<-socketToHandle z WriteMode hPutStrLn h$show$b a hClose h g s b 0=0 b x=x.&.0xFF+b(x`shiftR`8) 

Sadly I had to use Network.Socket to get access to the remote IP address as an integer rather than a string. It would have saved dozens of characters if I could just do s <- listenOn (PortNumber n), rather than having to explicitly call socket, bind and listen individually. But, sadly, Network.accept gives me a host string, not an IP address integer, so I had to resort to Network.Socket.accept and friends.

The function f takes a port number as argument, and creates a server socket (s) listening on that port. It then calls the function g with the server socket. g loops forever, accepting connections. The function b takes an actual IPv4 address and computes the sum of its digits.

I'm sure somebody somewhere can do this better than me. I wanted to show off just how damned easy socket stuff is in Haskell... but then failed miserably, because I need access to the IP address, which isn't usually easy to get.