2

I am porting a program which uses posix sockets to work in windows. I am not particularly familiar with sockets and so am learning as I go. I am aware of some of the required changes based on various sources, e.g. here and this stackoverflow question. However, I am wondering whether there is any commonly used header or code sample which abstracts away some of the differences.

The kind of thing I am imagining is a header with defines which map the WSA* error codes to the BSD equivalents where they exist, has macros for calling errno, perrno or WSAGetLastError. as required, and that kind of thing. Anything which eases the process of making this cross-platform.

I am aware of full fledged alternatives like boost::asio, but that would mean reimplementing everything which I do not want to do

Does anything like this exist? It will need to be under a GPL compatible library.

  • 1
    Are you sure you're looking for something in C++ though? – einpoklum Jun 21 '17 at 8:06
  • So, you've looked into all items on the long list on the SO question you linked to - and nothing is satisfactory? I doubt people here will be able to find something else beyond that... – einpoklum Jun 21 '17 at 8:09
  • 1
    @einpoklum, all of those things in the list are frameworks which require a complete rewrite of the socket code. I'm looking for a few headers which ease the process of porting existing code which uses BSD sockets to also work using winsock by adding #defines and some macros etc. I find it hard to believe I'm the first person to need this! – crobar Jun 21 '17 at 11:30
  • Well, I would actually guess people who face that challenge usually either develop their own framework or use an existing one rather than taking the approach you're hoping for. But who knows, I might be wrong. – einpoklum Jun 21 '17 at 13:52
1

The wxWidgets C++ library provides an abstraction of sockets that allows you to use a single code base for cross platform socket clients & servers: wxSocket Inheritance

This allows you to code without compile time switches in your code.

Of course wxWidgets is best known for providing a cross platform GUI but it also provides a number of cross platform abstractions and utility functions such as this, file system abstractions, date/time, etc. - not all of them have to be used from within the normal wxWidgets event loop and can be used standalone.

wxWidgets is free & open source licence is wxWindows Library Licence which is broadly MIT compatible with a free to distribute resulting binaries under any terms clause.

  • I think this is just another "full-fledged alternative", to use the author's term, rather than a wrapper around os-specific facilities. – einpoklum Jun 21 '17 at 8:05
  • Yes, this suggestion would require a full rewrite of the existing BSD sockets based code using the new wx sockets framework. I just want a few headers to help me port the existing code to also be able to use winsock. – crobar Jun 21 '17 at 11:32
  • 1
    @crobar - you could take a look at how wxWidgets__ achieves the cross platform sockets, IIRC it mostly uses Posix error values, function calls, etc., and maps the windows specific errors, function calls, etc. to Posix rather than the other way around. – Steve Barnes Jun 22 '17 at 6:00
  • @SteveBarnes, I'm looking into it, there is some useful stuff, might still mark this as the answer, or closest to it. – crobar Jun 22 '17 at 8:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.