MIT licensed by Lars Petersen
This version can be pinned in stack with:socket-,3175
Depends on 2 packages(full list with versions):


Available on Hackage License MIT Build Status


This library aims to expose a minimal and cross platform interface for POSIX compliant networking code.

Implementation Philosophy

  • Every operation and every flag exposed should be supported with same semantics on every platform. If this cannot be guaranteed it should be supplied by another (extension) package.

  • Absolutely no conditional exports.

  • No #ifdef madness in the Haskell sources. The Haskell binding code uses the FFI to reference platform dependant C functions for each operation. If a platform is not POSIX compliant (i.e. Windows) equivalent functionality is implemented by using whatever the platform specific building blocks are.

Platform Support


Platform is fully supported. Each commit and release is automatically tested with Travis CI and several versions of GHC.


Fully supported on Windows7 (maybe Vista) or higher :-)

GHC’s runtime system on Windows does not offer an event notification mechanism for sockets. The original network library suffers from this, too. For example, connection attempts are non-interruptible etc. The approach taken to circumvent this in this library is to poll the non-blocking sockets with increasing delay. This guarantees non-interruptability and fairness between different threads. It allows for decent throughput while also keeping CPU consumption on a moderate level if a socket has not seen events for a longer period of time (maximum of 1 second delay after 20 polling iterations). The only drawback is potentially reduced response time of your application. The good part: Heavy load (e.g. connection requests or incoming traffic) will reduce this problem. Eventually your accepting thread won’t wait at all if there are several connection requests queued.

This workaround may be removed if someone is willing to sacrifice to improve the IO manager on Windows.

Each release is manually tested on a Windows 10 virtual machine with the latest Haskell Platform (64bit).


Working, but not regularly tested.

Please report when it is no longer working on MacOS.


  • base
  • bytestring


The project uses tasty for testing.

There are two test suites: default and threaded which are using the same code. Only difference is that one is compiled against GHC’s single threaded RTS and the other against the multi-threaded one. Run cabal test to run both in sequence.

In order to see details and colored output you may also want to try

ghc --make test/test.hs && ./test/test

Changes Lars Petersen 2016-08-11

  • A potential race condition has been fixed (issue #18): c_get_last_error was supposed to return the error code of the last operation (if any). On Linux it just returned errno whereas on Windows it wrapped a call to WSAGetLastError. The problem was that the value of errno and WSAGetLastError is only valid when sampled immediately after the failed call. This could not be easily guaranteed the way it was implemented: GHC’s RTS is potentially allowed to interrupt the thread between the failed call and the call to c_get_last_error (although this is very unlikely when no memory allocation is necessary). The content of errno might have been reset of overridden by another thread. The solution for this is that all FFI calls now take a pointer with a reserved memory location (allocated on the stack, so it’s quite cheap) and the C functions immediately save the errno (if necessary). The unsafe ccalls are guaranteed to be uninterruptible.

  • All tests have been ported to tasty as previously proposed by Roman Cheplyaka.

  • Fixed connect operation to use getsockopt with SO_ERROR to determine socket connection status / error code instead of issuing a second connection attempt (see issue #15). On Windows, the solution is a bit more difficult: getsockopt return 0 unless the operation has either succeeded or failed. Unfortunately, there did not exist a mechanism to wait for this condition (GHC’s IO manager lacks this feature). This has been circumvented by calling select for the socket with minimal timeout several times with an exponential back-off. Tests have been added to validate different aspects of this. Lars Petersen 2016-04-10

  • Adapted the AddrInfo test suite to not depend on specific name resolution features that aren’t available in a chroot() environment (see issue #12). Lars Petersen 2016-03-26

  • Improved and adapted documentation.
  • Merged GetSocketOption and SetSocketOption to one single type class SocketOption.
  • getNameInfo now returns NameInfo instead of a tuple.
  • Added all theoretically possible SocketExceptions.
  • The type class GetNameInfo has been renamed to HasNameInfo.
  • The type class GetAddressInfo has been renamed to HasAddressInfo.
  • Removed operation withConnectedSocket without replacement. It should not be part of this minimal library. Its code can be retrieved from the repository if needed.
  • The operations sendAll and receiveAll are now exported through System.Socket.Type.Stream and no longer trough the main module. They are very specific, solely stream-oriented and just wrappers around the basic operations. Such operations shouldn’t pollute the main module.
  • Issue #10: Ben Gamari reported that the associated type SocketAddress is not injective which would lead to compilation failure on GHC 8.* . This is fixed by using a data family instead. Lars Petersen 2015-08-09

  • Added a test for eOperationNotSupported (try to listen on a UDP socket).
  • Niklas Hambüchen added eOperationNotSupported. Lars Petersen 2015-07-08

  • Don’t set msgNoSignal automatically with send and sendTo. This implicit behaviour is a bad design decision. The implications of this change are rather limited. The behaviour/correctness of an application is only affected if it hooked SIGPIPE. GHC’s RTS by default ignores SIGPIPE since #1619. You’re still advised to adapt your applications to use msgNoSignal explicitly when writing on stream oriented sockets. Otherwise the RTS gets unnecessarily interrupted. This is harmless, but annoying and not desired when developing high-performance applications.
  • Define msgNoSignal as 0 if not available and documented this behaviour.
  • Added new exception value ePipe. Lars Petersen 2015-06-22

  • Exposed unsafeGetSocketOption and unsafeSetSocketOption.
  • Exposed socketWaitRead and socketWaitWrite through System.Socket.Unsafe. Lars Petersen 2015-06-19

  • Introduced newtypes Port, FlowInfo and ScopeId in Inet6 family.
  • Renamed nearly everything in response to very constructive criticism by Bryan O’Sullivan. This is a breaking change (sorry about that). I felt this was the last chance to get this straight before the library gets widely adopted. Additional kudos to @ignorantone and @whatsthepoint.
  • Issue #7: Typo in documentation of inaddrNONE and inaddrBROADCAST. Kudos to Michael Fox. Lars Petersen 2015-06-17

  • tar-ball did not contain relevant source files. Lars Petersen 2015-06-16

  • Changed semantics of connect operation. It now blocks until a connection has either has been established or failed.
  • Added SO_ERROR socket option.
  • Added eALREADY exception constant.
  • Added eISCONN exception constant.
  • Added eNOTCONN exception constant.
  • Added convenience operation withConnectedSocket.
  • Added eNETUNREACH exception constant.
  • Added new operation recvAll and changed sendAll to lazy ByteString.
  • Added new socket option IPV6_V6ONLY.
  • Removed untested socket option SO_ACCEPTCONN.
  • Correctly defining AI_ flags on Windows (MinGW doesn’t although they are all well support on Vista or higher).
  • Got all tests passing on Windows 7. Lars Petersen 2015-06-07

  • Fixed documentation of eaiNONAME.
  • Fixed typo in .cabal file in reference to cbits file. Lars Petersen 2015-06-07

  • AddrInfoFlags and NameInfoFlags are now instances of Bits.
  • Dropped all sendmsg/recvmsg related operations (harden the core first!)
  • Dropped support for UNIX socket (will be separate package socket-unix)
  • Renamed type function Address to SockAddr.
  • Added GetAddrInfo and GetNameInfo classes.
  • Dropped support for SCTP (will be separate package socket-sctp)
  • Added support for RAW sockets.
  • Started to support Windows (still unfinished).
  • New operation recvRecord.
  • ReceiveMsg now returns a strict ByteString.
  • New operations sendV, sendToV.
  • Restricted getAddrInfo and getNameInfo and added getAddrInfo6 and getNameInfo6
  • Added address family types INET, INET6 and UNIX (API breaking change)
  • Hide SockAddrIn6 address constructor
  • Hide SockAddrIn address constructor
  • Added recvMsg operation
  • Fixed unsafeSend, unsafeSendTo and unsafeSendMsg (they were waiting for a read event instead of waiting for writing)
  • Use aiStrError values in Show instance
  • Added aiStrError function
  • Added constants for AddrInfoException
  • Changed definitin of AddrInfoException
  • Added sendAllMsg operation
  • Added sendMsg operation (+ some types and internals) Lars Petersen 2015-05-29

  • Added a sendAll operation
  • Exposed the Socket constructor
  • Added getNameInfo operation
  • Added msgWAITALL and fixed serious bug regarding all other MsgFlags
  • Nicer Show instances for SockAddrIn and SockAddrIn6
  • Hiding internal modules
  • Added getAddrInfo operation Lars Petersen 2015-05-28

  • Added
  • Removed threadWaitReadMVar and threadWaitWriteMVar
  • Import Data.Monoid in System.Socket.Unsafe to support older Preludes Lars Petersen 2015-05-28

  • Initial release