4

In windows, sockets need to be initialized, as shown in Networks.

On Windows operating systems, the networking subsystem has to be initialised using withSocketsDo before any networking operations can be used. eg.

  main = withSocketsDo $ do {...}

Although this is only strictly necessary on Windows platforms, it is harmless on other platforms, so for portability it is good practice to use it all the time.

What's special about windows?

Community
  • 1
  • 1
MdxBhmt
  • 1,310
  • 8
  • 16

2 Answers2

8

In existing versions of the network library, withSocketsDo is used to initialize the Winsock library, which is only a requirement on Windows. On other platforms no library needs initializing, so withSocketsDo does nothing.

In future versions of the network library, withSocketsDo is called automatically, so only needs to be included for compatibility with older versions, see this blog post for the details behind the changes.

Neil Mitchell
  • 9,090
  • 1
  • 27
  • 85
4

Windows, unlike other platforms, requires processes to kickstart their network connectivity by manually initializing WinSock.dll. Meanwhile, Haskell, unlike other languages, by design does not have global mutable state. Thus, the WinSock initialization can't be hidden inside the load of a library or creation of some singleton object, and instead needs to be registered manually by an explicit call.

Christian Conkle
  • 5,932
  • 1
  • 28
  • 52
sclv
  • 38,665
  • 7
  • 99
  • 204
  • 2
    The `network` library could *enforce* usage of something like `withSocketsDo` with any of several well-known techniques, such as wrapping all its operations in `Network a` rather than `IO a`. `withSocketsDo` dates back at least to 2006; a more modern library would be designed differently. – Christian Conkle Feb 24 '15 at 20:37
  • 3
    Forcing people into another monad just to use network ops sounds horrible to me! We could use implicit params or enforce that all functions took a "proof" of an initialization, but that likewise seems very heavy-handed. Personally I think the current design has the advantage of being extremely lightweight and unobtrusive. – sclv Feb 24 '15 at 23:49
  • 3
    I don't disagree with you. The "problem" here is that the necessity of `withSocketsDo` is *only* captured in the human-readable docs. A high-level library, which `network` isn't, should either conceal initialization or enforce it as an invariant. – Christian Conkle Feb 25 '15 at 18:38
  • 4
    The current design is awful, so I fixed it a few months ago (recently merged). There was nothing deep to fix, it really was just an ugly and unnecessary wart. – Neil Mitchell Feb 25 '15 at 22:16
  • 1
    @Christian That's a kind of design question that is bothering me now, too (for another library I'm planning to work on). I have [read about such techniques](http://stackoverflow.com/a/14145412/94687): requiring a token or additionally wrapping in a monad. The guarantees they give are attractive, but a monad like `Network a` ontop of `IO a` looks dull semantically, because it completely repeats the semantics of `IO`. Passing tokens looks a bit dangerous from the conceptual view because it can be understood as passing "world" and allowing non-linear uses of world states, which the actions modify – imz -- Ivan Zakharyaschev Feb 27 '15 at 13:42
  • In my planned library, there is a singleton object which must be initialized (tokens are more or less satisfactory in this case), and another class of objects with state (with a fixed set of actions you can do with them). Since the set of basic actions is fixed, creating a special monad for working with them seems nice, but mixing the work with several such objects if they are represented as monads must be a nightmare (think of stacking all the monads of the same type). I've always felt that Haskell lacks some kind of a nice approach to such problems. (Acting on files via handles is also ugly) – imz -- Ivan Zakharyaschev Feb 27 '15 at 13:51
  • @imz--IvanZakharyaschev - Those are all good points/questions; there are solutions to some but not all of them. This isn't the right place to discuss, though; perhaps a question on Programmers.SE? – Christian Conkle Feb 27 '15 at 17:31