2012-07-25 293 views
0

VxWorks設置FIONBIO的標準方法是使用ioctl()而不是fcntl()。對於FIONBIO文檔給這個作爲一個例子,這顯然是不會編譯,因爲on具有任何數據類型:如何使用ioctl()在VxWorks中的套接字上設置FIONBIO?

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

我看到周圍的說,使用這樣的事情(淨例如使用其在本質上是相同的):

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

然而,文件說,該原型ioctl()ioctl(int, int, int),而我得到無法錯誤int*轉換爲int。如果我將該值作爲int傳遞,我只會得到一個致命的內核任務級別異常。

這是我當前的代碼:

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

返回錯誤:

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

回答

2

Found it here.

看起來我只需要將int*投射到int。我不能使用C風格的鑄造,所以我使用reinterpret_cast

int SetBlocking(int sockfd, bool blocking) 
{ 
    int nonblock = !blocking; 

    return ioctl(sockfd, 
    FIONBIO, 
    reinterpret_cast<int>(&nonblock)); 
} 
相關問題