Search code examples
c++socketsvxworks

How do I use ioctl() to set FIONBIO on a socket in VxWorks?


VxWorks's standard method to set FIONBIO is with ioctl(), not fcntl(). The documentation for FIONBIO gives this as an example, which obviously isn't going to compile, because on has no datatype:

on = TRUE;
status = ioctl (sFd, FIONBIO, &on);

I've seen example usage around the net that says to use something like this (which is essentially the same thing):

int on = 1;
ioctl(fd, FIONBIO, &on);

However, the documentation says the prototype for ioctl() is ioctl(int, int, int), and I get errors about unable to convert int* to int. If I pass the value as an int, I just get a fatal kernel task-level exception.

This is my current code:

int SetBlocking(int sockfd, bool blocking)
{
  int nonblock = !blocking;
  return ioctl(sockfd, FIONBIO, &nonblock);
}

which returns the error:

error: invalid conversion from `int*' to `int'
initializing argument 3 of `int ioctl(int, int, int)'

Solution

  • Found it here.

    Looks like I just need to cast the int* to int. I can't use c-style casting, so I used reinterpret_cast.

    int SetBlocking(int sockfd, bool blocking)
    {
      int nonblock = !blocking;
    
      return ioctl(sockfd,
        FIONBIO,
        reinterpret_cast<int>(&nonblock));
    }