ioctlSocket in VB.NET
-
Hello everybody. I want to use the ioctlsocket winsock function to set non blocking mode to a socket in VB.NET. I have to use this function because it seems there is no means to create a Net.sockets.socket object from a valid Win32 SOCKET handle (provided by a custom function librairy) In my code, I alway Have SOCKET_ERROR after ioctlsocket call ... and lastError is set to 10022 Maybe my FIONBIO constant is not correct. Can someone help me ? tks
// C Computing of FIONBIO : //#define IOCPARM_MASK 0x7f /*parameters must be < 128 bytes */ //#define IOC_IN 0x80000000 /* copy in parameters */ //#define _IOW(x,y,t) (IOC_IN|(((long)sizeof(t)&IOCPARM_MASK)<<16)|((x)<<8)|(y)) //#define FIONBIO _IOW('f', 126, u_long) /* set/clear non-blocking i/o */ Private Shared ul As IntPtr = Marshal.AllocHGlobal(4) Private Declare Function ioctlsocket Lib "ws2_32.dll" (ByVal sck As Long, ByVal cmd As Long, <[In](), Out()> ByVal argp As IntPtr) As Integer Public Shared Sub SetNonBlockingMode(ByVal sck As Long) 'Dim ui As UInt32 = UInt32.Parse(1) 'Marshal.StructureToPtr(ui, ul, False) Marshal.WriteInt32(ul, 0) LastErrorValue = 0 Dim iRes As Long = ioctlsocket(sck, 2147772030, ul) 'Valeur de FIONBIO sur mon PC = 2147772030 If iRes = SOCKET_ERROR Then LastErrorValue = Marshal.GetLastWin32Error() Throw New Net.Sockets.SocketException(LastErrorValue) End If End Sub