codeplea/Hands-On-Network-Programming-with-C

setsockopt won't compile on VC++

Opened this issue · 3 comments

The call to setsockopt won't compiler on VC++ compiler since (on Windows headers), setsockopt takes char* and not void*. It is true that MS header/implmenetation is faulty as it doesn't make sense to take char* - but the code won't compile.

int option = 0;
setsockopt(socket_listen, IPPROTO_IPV6, IPV6_V6ONLY,(void*)&option, sizeof(option));

Hi Ajay,

It looks like this code is used in both time_server_dual.c and server_reuse.c.

The error you are describing would only happen if you are compiling the code as C++. Can you try to compile it as C code instead? I've tested it with VS2015 and VS2017, as long as it's compiled as C code, it should work.

I will work on a fix for the example programs to make it compatible with C++. I think this will require a macro so it works on both Windows and Linux.

Thanks for bringing this to my attention.

I know that the book is targeted for C programmers, to make the most use of the code samples given. However, in reality, the code would be used in C++ programs, possibly inside some class; hence code should be C++ (and C) complaint. :)

That is very true. I tried very hard to make all of the code C and C++ valid. I just missed this case.

I will try to update the code soon with a fix.