Verified Commit 992bd890 authored by Allele Dev's avatar Allele Dev

docs: add examples to readme

parent ad2a9986
......@@ -19,6 +19,58 @@ properties at the type level. Currently, the following are tracked:
## Examples
Imports:
``` haskell
import Network.Typed.Socket
import Network.Socket (tupleToHostAddress)
```
Setting a TCP/IPv4 server address:
``` haskell
serverAddress = SockAddrInet 2291 (tupleToHostAddress (127,0,0,1))
```
An echo server:
```haskell
-- point-free style
pfServer =
tcp4Socket
>>= bind serverAddress
>>= listen 1
>>= (\server ->
accept server >>= (\(client, _) ->
recv 32 client >>= (\bs -> send bs client)))
-- explicit do-notation
doServer = do
sock <- tcp4Socket
bound <- bind serverAddress sock
server <- listen 1 bound
(client, _) <- accept server
bs <- recv 32 client
send bs client
```
An echo client:
``` haskell
-- point-free style
pfClient =
tcp4Socket
>>= connect serverAddress
>>= (\client -> send "fish" client >> recv 32 client)
-- explicit do-notation
doClient = do
sock <- tcp4Socket
client <- connect serverAddress sock
send "fish" client
recv 32 client
```
## Contributing
Contributions are welcome! Documentation, examples, code, and
......
......@@ -104,22 +104,22 @@ socket =
in SSocket <$> (NS.socket x y NS.defaultProtocol)
tcp4Socket :: IO (SSocket 'InetV4 'Tcp 'Unconnected 'Available)
tcp4Socket = (socket :: IO (SSocket 'InetV4 'Tcp 'Unconnected 'Available))
tcp4Socket = socket
tcp6Socket :: IO (SSocket 'InetV6 'Tcp 'Unconnected 'Available)
tcp6Socket = (socket :: IO (SSocket 'InetV6 'Tcp 'Unconnected 'Available))
tcp6Socket = socket
tcpUnixSocket :: IO (SSocket 'Unix 'Tcp 'Unconnected 'Available)
tcpUnixSocket = (socket :: IO (SSocket 'Unix 'Tcp 'Unconnected 'Available))
tcpUnixSocket = socket
udp4Socket :: IO (SSocket 'InetV4 'Udp 'Unconnected 'Available)
udp4Socket = (socket :: IO (SSocket 'InetV4 'Udp 'Unconnected 'Available))
udp4Socket = socket
udp6Socket :: IO (SSocket 'InetV6 'Udp 'Unconnected 'Available)
udp6Socket = (socket :: IO (SSocket 'InetV6 'Udp 'Unconnected 'Available))
udp6Socket = socket
udpUnixSocket :: IO (SSocket 'Unix 'Udp 'Unconnected 'Available)
udpUnixSocket = (socket :: IO (SSocket 'Unix 'Udp 'Unconnected 'Available))
udpUnixSocket = socket
-- bracketed, typed helpers
withSocket :: (SockFam f, SockProto p) => (SSocket f p 'Unconnected 'Available -> IO a) -> IO a
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment