Preprocessor check for availability of Winsockets / BSD sockets

51 views Asked by At

I'm new to socket programming and want to make a cross platform app (C++) that supports both windows and unix systems.

The default on for websocket transport on Windows seems to be windows sockets while on unix systems it's BSD sockets. So how can I decide at preprocessor time which sockets are available so I can abstract away this layer? Is there a definition which I can check which also corresponds to the availability of these features?

I thought about checking the _WIN32 and __unix__ definitions but those are platform definitions and I'm not sure if they necessarily correspond to the availability of a feature. I guess this is very well the case for Windows systems but I heard that some unix systems will only offer partial BSD socket compliance so this check could be misleading?

1

There are 1 answers

2
Remy Lebeau On

I'm new to socket programming and want to make a cross platform app (C++) that supports both windows and unix systems.

There are numerous cross-platform socket libraries that are already available.

The default on for websocket transport on Windows seems to be windows sockets while on unix systems it's BSD sockets.

Winsock includes BSD-compatible socket functions that are mostly API-compatible with other platforms (there are some differences). The biggest difference you will have to deal with is in the header files that you will need to #include on each platform.

Is there a definition which I can check which also corresponds to the availability of these features?

No, there is not.

I thought about checking the _WIN32 and __unix__ definitions but those are platform definitions and I'm not sure if they necessarily correspond to the availability of a feature.

That is basically what you need to do (and what other cross-platform libraries do).