ホーム › Networking.WinSock › ioctlsocket
ioctlsocket
関数ソケットの入出力モードを制御する。
シグネチャ
// WS2_32.dll
#include <windows.h>
INT ioctlsocket(
SOCKET s,
INT cmd,
DWORD* argp
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| s | SOCKET | in | socket を識別するディスクリプタです。 |
| cmd | INT | in | socket s に対して実行するコマンドです。Winsock IOCTLs を参照してください。 |
| argp | DWORD* | inout | cmd のパラメータへのポインタです。 |
戻り値の型: INT
公式ドキュメント
ioctlsocket 関数 (winsock.h) は、socket の I/O モードを制御します。
戻り値
正常に完了すると、 ioctlsocket はゼロを返します。それ以外の場合は SOCKET_ERROR が返され、 WSAGetLastError を呼び出すことで具体的なエラーコードを取得できます。
| エラーコード | 意味 |
|---|---|
| この関数を使用する前に、 WSAStartup の呼び出しが正常に完了している必要があります。 | |
| ネットワークサブシステムで障害が発生しました。 | |
| ブロッキング型の Windows Sockets 1.1 呼び出しが進行中であるか、サービスプロバイダがコールバック関数の処理中です。 | |
| ディスクリプタ s は socket ではありません。 | |
| argp パラメータがユーザーアドレス空間の有効な部分を指していません。 |
解説(Remarks)
ioctlsocket 関数は、任意の状態にある任意の socket に対して使用できます。プロトコルや通信サブシステムとは独立して、socket に関連付けられた一部の動作パラメータを設定または取得するために使用します。cmd パラメータで使用できるコマンドとその意味は次のとおりです。
WSAIoctl 関数は、socket、トランスポートプロトコル、または通信サブシステムに関連付けられた動作パラメータを設定または取得するために使用します。
WSAIoctl 関数は ioctlsocket 関数よりも強力で、設定または取得する動作パラメータに対して非常に多くの値をサポートします。
サンプルコード
次の例は、ioctlsocket 関数の使用方法を示しています。
#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);
}
互換性
この ioctlsocket 関数は、Berkeley sockets の ioctl 関数と比較すると、socket に対して一部の機能しか実行しません。 ioctlsocket 関数には ioctl の FIOASYNC に相当するコマンドパラメータがなく、 ioctlsocket がサポートする socket レベルのコマンドは SIOCATMARK のみです。Windows Phone 8: この関数は、Windows Phone 8 以降の Windows Phone Store アプリでサポートされています。
Windows 8.1 および Windows Server 2012 R2: この関数は、Windows 8.1、Windows Server 2012 R2 以降の Windows Store アプリでサポートされています。
出典・ライセンス: 上記「公式ドキュメント」の内容は Microsoft の Win32 API ドキュメント(MicrosoftDocs/sdk-api)を日本語に翻訳・改変したものです。© Microsoft Corporation. CC BY 4.0 で提供。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
各言語での呼び出し定義
// WS2_32.dll
#include <windows.h>
INT ioctlsocket(
SOCKET s,
INT cmd,
DWORD* argp
);[DllImport("WS2_32.dll", SetLastError = true, ExactSpelling = true)]
static extern int ioctlsocket(
UIntPtr s, // SOCKET
int cmd, // INT
ref uint argp // DWORD* in/out
);<DllImport("WS2_32.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function ioctlsocket(
s As UIntPtr, ' SOCKET
cmd As Integer, ' INT
ByRef argp As UInteger ' DWORD* in/out
) As Integer
End Function' s : SOCKET
' cmd : INT
' argp : DWORD* in/out
Declare PtrSafe Function ioctlsocket Lib "ws2_32" ( _
ByVal s As LongPtr, _
ByVal cmd As Long, _
ByRef argp As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。import ctypes
from ctypes import wintypes
ioctlsocket = ctypes.windll.ws2_32.ioctlsocket
ioctlsocket.restype = ctypes.c_int
ioctlsocket.argtypes = [
ctypes.c_size_t, # s : SOCKET
ctypes.c_int, # cmd : INT
ctypes.POINTER(wintypes.DWORD), # argp : DWORD* in/out
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('WS2_32.dll')
ioctlsocket = Fiddle::Function.new(
lib['ioctlsocket'],
[
Fiddle::TYPE_UINTPTR_T, # s : SOCKET
Fiddle::TYPE_INT, # cmd : INT
Fiddle::TYPE_VOIDP, # argp : DWORD* in/out
],
Fiddle::TYPE_INT)#[link(name = "ws2_32")]
extern "system" {
fn ioctlsocket(
s: usize, // SOCKET
cmd: i32, // INT
argp: *mut u32 // DWORD* in/out
) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.$sig = @"
[DllImport("WS2_32.dll", SetLastError = true)]
public static extern int ioctlsocket(UIntPtr s, int cmd, ref uint argp);
"@
$api = Add-Type -MemberDefinition $sig -Name 'WS2_32_ioctlsocket' -Namespace Win32 -PassThru
# $api::ioctlsocket(s, cmd, argp)#uselib "WS2_32.dll"
#func global ioctlsocket "ioctlsocket" sptr, sptr, sptr
; ioctlsocket s, cmd, varptr(argp) ; 戻り値は stat
; s : SOCKET -> "sptr"
; cmd : INT -> "sptr"
; argp : DWORD* in/out -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。出力引数:
#uselib "WS2_32.dll" #cfunc global ioctlsocket "ioctlsocket" sptr, int, var ; res = ioctlsocket(s, cmd, argp) ; s : SOCKET -> "sptr" ; cmd : INT -> "int" ; argp : DWORD* in/out -> "var" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。#uselib "WS2_32.dll" #cfunc global ioctlsocket "ioctlsocket" sptr, int, sptr ; res = ioctlsocket(s, cmd, varptr(argp)) ; s : SOCKET -> "sptr" ; cmd : INT -> "int" ; argp : DWORD* in/out -> "sptr" ; ※出力/バッファ引数はポインタ方式(token=sptr / 呼び出しは varptr(変数))。
出力引数:
; INT ioctlsocket(SOCKET s, INT cmd, DWORD* argp) #uselib "WS2_32.dll" #cfunc global ioctlsocket "ioctlsocket" intptr, int, var ; res = ioctlsocket(s, cmd, argp) ; s : SOCKET -> "intptr" ; cmd : INT -> "int" ; argp : DWORD* in/out -> "var" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。; INT ioctlsocket(SOCKET s, INT cmd, DWORD* argp) #uselib "WS2_32.dll" #cfunc global ioctlsocket "ioctlsocket" intptr, int, intptr ; res = ioctlsocket(s, cmd, varptr(argp)) ; s : SOCKET -> "intptr" ; cmd : INT -> "int" ; argp : DWORD* in/out -> "intptr" ; ※出力/バッファ引数はポインタ方式(token=intptr / 呼び出しは varptr(変数))。
import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
ws2_32 = windows.NewLazySystemDLL("WS2_32.dll")
procioctlsocket = ws2_32.NewProc("ioctlsocket")
)
// s (SOCKET), cmd (INT), argp (DWORD* in/out)
r1, _, err := procioctlsocket.Call(
uintptr(s),
uintptr(cmd),
uintptr(argp),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // INTfunction ioctlsocket(
s: NativeUInt; // SOCKET
cmd: Integer; // INT
argp: Pointer // DWORD* in/out
): Integer; stdcall;
external 'WS2_32.dll' name 'ioctlsocket';result := DllCall("WS2_32\ioctlsocket"
, "UPtr", s ; SOCKET
, "Int", cmd ; INT
, "Ptr", argp ; DWORD* in/out
, "Int") ; return: INT●ioctlsocket(s, cmd, argp) = DLL("WS2_32.dll", "int ioctlsocket(int, int, void*)")
# 呼び出し: ioctlsocket(s, cmd, argp)
# s : SOCKET -> "int"
# cmd : INT -> "int"
# argp : DWORD* in/out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。const std = @import("std");
extern "ws2_32" fn ioctlsocket(
s: usize, // SOCKET
cmd: i32, // INT
argp: [*c]u32 // DWORD* in/out
) callconv(std.os.windows.WINAPI) i32;proc ioctlsocket(
s: uint, # SOCKET
cmd: int32, # INT
argp: ptr uint32 # DWORD* in/out
): int32 {.importc: "ioctlsocket", stdcall, dynlib: "WS2_32.dll".}pragma(lib, "ws2_32");
extern(Windows)
int ioctlsocket(
size_t s, // SOCKET
int cmd, // INT
uint* argp // DWORD* in/out
);ccall((:ioctlsocket, "WS2_32.dll"), stdcall, Int32,
(Csize_t, Int32, Ptr{UInt32}),
s, cmd, argp)
# s : SOCKET -> Csize_t
# cmd : INT -> Int32
# argp : DWORD* in/out -> Ptr{UInt32}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。local ffi = require("ffi")
ffi.cdef[[
int32_t ioctlsocket(
uintptr_t s,
int32_t cmd,
uint32_t* argp);
]]
local ws2_32 = ffi.load("ws2_32")
-- ws2_32.ioctlsocket(s, cmd, argp)
-- s : SOCKET
-- cmd : INT
-- argp : DWORD* in/out
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。const koffi = require('koffi');
const lib = koffi.load('WS2_32.dll');
const ioctlsocket = lib.func('__stdcall', 'ioctlsocket', 'int32_t', ['uintptr_t', 'int32_t', 'uint32_t *']);
// ioctlsocket(s, cmd, argp)
// s : SOCKET -> 'uintptr_t'
// cmd : INT -> 'int32_t'
// argp : DWORD* in/out -> 'uint32_t *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("WS2_32.dll", {
ioctlsocket: { parameters: ["usize", "i32", "pointer"], result: "i32" },
});
// lib.symbols.ioctlsocket(s, cmd, argp)
// s : SOCKET -> "usize"
// cmd : INT -> "i32"
// argp : DWORD* in/out -> "pointer"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
int32_t ioctlsocket(
size_t s,
int32_t cmd,
uint32_t* argp);
C, "WS2_32.dll");
// $ffi->ioctlsocket(s, cmd, argp);
// s : SOCKET
// cmd : INT
// argp : DWORD* in/out
// 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
// WINAPI(stdcall): x64 では呼出規約が統一されるため問題なし。x86 では __stdcall 対応のラッパが必要な場合あり。import com.sun.jna.*;
import com.sun.jna.ptr.*;
import com.sun.jna.win32.StdCallLibrary;
import com.sun.jna.win32.W32APIOptions;
public interface Ws2_32 extends StdCallLibrary {
Ws2_32 INSTANCE = Native.load("ws2_32", Ws2_32.class);
int ioctlsocket(
long s, // SOCKET
int cmd, // INT
IntByReference argp // DWORD* in/out
);
}@[Link("ws2_32")]
lib LibWS2_32
fun ioctlsocket = ioctlsocket(
s : LibC::SizeT, # SOCKET
cmd : Int32, # INT
argp : UInt32* # DWORD* in/out
) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。import 'dart:ffi';
import 'package:ffi/ffi.dart';
typedef ioctlsocketNative = Int32 Function(UintPtr, Int32, Pointer<Uint32>);
typedef ioctlsocketDart = int Function(int, int, Pointer<Uint32>);
final ioctlsocket = DynamicLibrary.open('WS2_32.dll')
.lookupFunction<ioctlsocketNative, ioctlsocketDart>('ioctlsocket');
// s : SOCKET -> UintPtr
// cmd : INT -> Int32
// argp : DWORD* in/out -> Pointer<Uint32>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function ioctlsocket(
s: NativeUInt; // SOCKET
cmd: Integer; // INT
argp: Pointer // DWORD* in/out
): Integer; stdcall;
external 'WS2_32.dll' name 'ioctlsocket';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "ioctlsocket"
c_ioctlsocket :: CUIntPtr -> Int32 -> Ptr Word32 -> IO Int32
-- s : SOCKET -> CUIntPtr
-- cmd : INT -> Int32
-- argp : DWORD* in/out -> Ptr Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let ioctlsocket =
foreign "ioctlsocket"
(size_t @-> int32_t @-> (ptr uint32_t) @-> returning int32_t)
(* s : SOCKET -> size_t *)
(* cmd : INT -> int32_t *)
(* argp : DWORD* in/out -> (ptr uint32_t) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)(cffi:define-foreign-library ws2_32 (t "WS2_32.dll"))
(cffi:use-foreign-library ws2_32)
(cffi:defcfun ("ioctlsocket" ioctlsocket :convention :stdcall) :int32
(s :uint64) ; SOCKET
(cmd :int32) ; INT
(argp :pointer)) ; DWORD* in/out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $ioctlsocket = Win32::API::More->new('WS2_32',
'int ioctlsocket(WPARAM s, int cmd, LPVOID argp)');
# my $ret = $ioctlsocket->Call($s, $cmd, $argp);
# s : SOCKET -> WPARAM
# cmd : INT -> int
# argp : DWORD* in/out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。関連項目
公式の関連項目
- f WSAAsyncSelect — ソケットのネットワークイベント通知をウィンドウメッセージで要求する。
- f WSAEventSelect — ソケットのネットワークイベントをイベントオブジェクトに関連付ける。
- f WSAIoctl — ソケットに対する制御操作を実行する。
- f getsockopt — ソケットオプションの値を取得する。
- f setsockopt — ソケットオプションの値を設定する。
- f socket — 指定したアドレスファミリ・型・プロトコルのソケットを作成する。