network-light
A slimmed down version of network
| Stackage Nightly 2026-08-08: | 0.1.0.6 |
| Latest on Hackage: | 0.1.0.6 |
network-light-0.1.0.6@sha256:5dc047721ab48f6c07b324a3b7a9834de0fe71196e8beb868a1815287ec1fe74,2274Module documentation for 0.1.0.6
- System
network-light
A small, portable subset of the network package’s socket API.
What this is
network-light implements a small subset of the socket functionality found in the network package: creating TCP/UDP sockets addressable via IPv4, connecting, binding, listening, accepting connections, and sending/receiving raw bytes, String, or ByteString.
The API deliberately mirrors network’s in spirit, so moving between the two should feel familiar.
It exists because MicroHs (mhs), a small Haskell compiler, cannot yet compile the full network package. network-light is implemented directly on top of the C socket()/connect()/send()/recv()/… calls via plain FFI imports (no C stubs) which keeps it simple enough to compile under both GHC and MicroHs from the same source. Until MicroHs can compile network in full, this package is the quickest way to get sockets working under both compilers.
This is not, and does not try to be, a replacement for network. It only implements what has been needed so far — Domain and SockOpt, for example, each model a handful of constructors, not the full POSIX surface.
Scope
- TCP (
SOCK_STREAM) and UDP (SOCK_DGRAM) sockets over IPv4 (AF_INET) connect,bind,listen,accept,close- Sending and receiving raw buffers,
String, orByteString, either “best effort” or looped until the full amount is sent/received - A handful of socket options:
SO_REUSEADDR,SO_DEBUG,SO_TYPE, and non-blocking mode - Sockets are non-blocking by default and integrate correctly with both GHC’s I/O manager and MicroHs’s cooperative, green-thread concurrency
- Compiles under GHC and MicroHs, on Linux; a
zephyrcabal flag selects the differingsockaddr_inlayout needed for Zephyr RTOS embedded targets
If you need something this package doesn’t have yet, such as IPv6, more socket options, Unix domain sockets, and so on, please fork it, add what you need, and open a pull request. Contributions are very welcome, as long as they keep to the existing style: plain FFI imports, no C stubs unless truly unavoidable, and code that compiles under both GHC and MicroHs.
Installation
cabal install network-light
or add it to your .cabal file:
build-depends: network-light
Example
module Main where
import System.Network
port :: Int
port = 4242
server :: IO ()
server = do
serverFd <- socket AF_INET SOCK_STREAM
setsocketopt serverFd SO_REUSEADDR 1
bind serverFd (mkSockAddr port Nothing)
listen serverFd 1
(clientFd, clientAddr) <- accept serverFd
putStrLn ("received connection from: " <> show clientAddr)
msg <- recvString clientFd 100
putStrLn msg
_ <- sendString clientFd "Hello, client!"
close clientFd
close serverFd
client :: IO ()
client = do
fd <- socket AF_INET SOCK_STREAM
connect fd (mkSockAddr port (Just "127.0.0.1"))
_ <- sendString fd "Hello, server!"
reply <- recvString fd 100
putStrLn reply
close fd
-- Run `server` in one terminal and `client` in another; they will talk to each other.
main :: IO ()
main = server
Testing
The test suite lives under tests/ and is driven by make rather than
cabal test, so that every test is built and run against both GHC and MicroHs from the same source:
cd tests
make test # run every test under both mhs and ghc
make test HC=ghc # GHC only
make test HC=mhs # MicroHs only
License
Apache License 2.0. See LICENSE.
Changes
Revision history for network-light
0.1.0.0 – YYYY-mm-dd
- First version. Released on an unsuspecting world.
0.1.0.1 – YYYY-mm-dd
- Just cleanups
0.1.0.2 – 2025-09-30
- Add README with example
- Small changes to make it compile with both mhs and ghc
0.1.0.4 – 2026-04-22
- Previous version only worked non-blockingly with GHC, not MHS (as MHS did not implement anything to combat non-blocking IO). I have added simple support for non-blocking IO in MHS, whereas a green thread may continue running while another is blocking on a call in this file.
- Refactoring work.
- Small changes to README
0.1.0.5 – 2026-07-16
- Made it work with GHC again, and added some tests.
- Worked on the Haddock documentation, before eventually putting it on Hackage.
0.1.0.6 – 2026-08-06
- Verified that it works with a bunch more GHC cmopilers.