c++visual-studio-2012setsockopt

How to set a socket option in C++ using setsockopt


Still, having troubles with my code.

if (argc > 0) {
int route (argc);//[argc+1]
((char*) route)[0] = 1;
((char*) route)[1] = 2;//131
((char*) route)[2] = 3 + argc * 4;
((char*) route)[3] = 4;
for (int i = 0; i < argc; i++) {
    route = inet_addr(argv[i]);
}

if (setsockopt(_socket.native_handle(), IPPROTO_IP, IP_OPTIONS, route, (argc + 1) * 4) < 0) {
    perror("can't set socket option");
}
}

here's a part of it, keep getting this error C2664: cannot convert parameter 4 from 'int' to 'const char *'


Solution

  • Microsoft's implementation of setsockopt() has a const char* for the fourth option. POSIX usually has a const void*. It has to be pointing to a buffer that contains values. The last argument is the size in bytes of the buffer.

    So something like this:

    setsockopt(
        _socket.native_handle(), IPPROTO_IP, IP_OPTIONS,
        reinterpret_cast<char*>(&route), sizeof(int));
    

    I don't know enough about sockets to tell you whether what you're passing actually makes sense. Here's the documentation on MSDN for IP_OPTIONS.