Skip to content

Latest commit

 

History

History
242 lines (177 loc) · 6.26 KB

nf-winsock2-ioctlsocket.md

File metadata and controls

242 lines (177 loc) · 6.26 KB
UID title description helpviewer_keywords old-location tech.root ms.assetid ms.date ms.keywords req.header req.include-header req.target-type req.target-min-winverclnt req.target-min-winversvr req.kmdf-ver req.umdf-ver req.ddi-compliance req.unicode-ansi req.idl req.max-support req.namespace req.assembly req.type-library req.lib req.dll req.irql targetos req.typenames req.redist ms.custom f1_keywords dev_langs topic_type api_type api_location api_name
NF:winsock2.ioctlsocket
ioctlsocket function (winsock2.h)
The ioctlsocket function (winsock2.h) controls the I/O mode of a socket and can be used on any socket in any state.
_win32_ioctlsocket_2
ioctlsocket
ioctlsocket function [Winsock]
winsock.ioctlsocket_2
winsock/ioctlsocket
winsock\ioctlsocket_2.htm
WinSock
048fcb8d-acd3-4917-a997-dd133db399f8
08/03/2022
_win32_ioctlsocket_2, ioctlsocket, ioctlsocket function [Winsock], winsock.ioctlsocket_2, winsock/ioctlsocket
winsock2.h
Winsock2.h
Windows
Windows 8.1, Windows Vista [desktop apps \| UWP apps]
Windows Server 2003 [desktop apps \| UWP apps]
Ws2_32.lib
Ws2_32.dll
Windows
19H1
ioctlsocket
winsock2/ioctlsocket
c++
APIRef
kbSyntax
DllExport
Ws2_32.dll
ioctlsocket

ioctlsocket function

-description

The ioctlsocket function controls the I/O mode of a socket.

-parameters

-param s [in]

A descriptor identifying a socket.

-param cmd [in]

A command to perform on the socket s.

-param argp [in, out]

A pointer to a parameter for cmd.

-returns

Upon successful completion, the ioctlsocket returns zero. Otherwise, a value of SOCKET_ERROR is returned, and a specific error code can be retrieved by calling WSAGetLastError.

Error code Meaning
WSANOTINITIALISED
A successful WSAStartup call must occur before using this function.
WSAENETDOWN
The network subsystem has failed.
WSAEINPROGRESS
A blocking Windows Sockets 1.1 call is in progress, or the service provider is still processing a callback function.
WSAENOTSOCK
The descriptor s is not a socket.
WSAEFAULT
The argp parameter is not a valid part of the user address space.

-remarks

The ioctlsocket function can be used on any socket in any state. It is used to set or retrieve some operating parameters associated with the socket, independent of the protocol and communications subsystem. Here are the supported commands to use in the cmd parameter and their semantics:

The WSAIoctl function is used to set or retrieve operating parameters associated with the socket, the transport protocol, or the communications subsystem.

The WSAIoctl function is more powerful than the ioctlsocket function and supports a large number of possible values for the operating parameters to set or retrieve.

Example Code

The following example demonstrates the use of the ioctlsocket function.
#include <winsock2.h>
#include <stdio.h>

#pragma comment(lib, "Ws2_32.lib")

void main()
{
//-------------------------
// Initialize Winsock
WSADATA wsaData;
int iResult;
u_long iMode = 0;

iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != NO_ERROR)
  printf("Error at WSAStartup()\n");

//-------------------------
// Create a SOCKET object.
SOCKET m_socket;
m_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (m_socket == INVALID_SOCKET) {
  printf("Error at socket(): %ld\n", WSAGetLastError());
  WSACleanup();
  return;
}

//-------------------------
// Set the socket I/O mode: In this case FIONBIO
// enables or disables the blocking mode for the 
// socket based on the numerical value of iMode.
// If iMode = 0, blocking is enabled; 
// If iMode != 0, non-blocking mode is enabled.

iResult = ioctlsocket(m_socket, FIONBIO, &iMode);
if (iResult != NO_ERROR)
  printf("ioctlsocket failed with error: %ld\n", iResult);
  

}

Compatibility

This ioctlsocket function performs only a subset of functions on a socket when compared to the ioctl function found in Berkeley sockets. The ioctlsocket function has no command parameter equivalent to the FIOASYNC of ioctl, and SIOCATMARK is the only socket-level command that is supported by ioctlsocket.

Windows Phone 8: This function is supported for Windows Phone Store apps on Windows Phone 8 and later.

Windows 8.1 and Windows Server 2012 R2: This function is supported for Windows Store apps on Windows 8.1, Windows Server 2012 R2, and later.

-see-also

WSAAsyncSelect

WSAEventSelect

WSAIoctl

Winsock Functions

Winsock Reference

getsockopt

setsockopt

socket