Win32 API 日本語リファレンス
ホームNetworking.WinSock › socket

socket

関数
指定したアドレスファミリ・型・プロトコルのソケットを作成する。
DLLWS2_32.dll呼出規約winapiSetLastErrorあり対応OSWindows 8.1 以降

シグネチャ

// WS2_32.dll
#include <windows.h>

SOCKET socket(
    INT af,
    WINSOCK_SOCKET_TYPE type,
    INT protocol
);

パラメーター

名前方向
afINTin
typeWINSOCK_SOCKET_TYPEin
protocolINTin

戻り値の型: SOCKET

各言語での呼び出し定義

// WS2_32.dll
#include <windows.h>

SOCKET socket(
    INT af,
    WINSOCK_SOCKET_TYPE type,
    INT protocol
);
[DllImport("WS2_32.dll", SetLastError = true, ExactSpelling = true)]
static extern UIntPtr socket(
    int af,   // INT
    int type,   // WINSOCK_SOCKET_TYPE
    int protocol   // INT
);
<DllImport("WS2_32.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function socket(
    af As Integer,   ' INT
    type As Integer,   ' WINSOCK_SOCKET_TYPE
    protocol As Integer   ' INT
) As UIntPtr
End Function
' af : INT
' type : WINSOCK_SOCKET_TYPE
' protocol : INT
Declare PtrSafe Function socket Lib "ws2_32" ( _
    ByVal af As Long, _
    ByVal type As Long, _
    ByVal protocol As Long) As LongPtr
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

socket = ctypes.windll.ws2_32.socket
socket.restype = ctypes.c_size_t
socket.argtypes = [
    ctypes.c_int,  # af : INT
    ctypes.c_int,  # type : WINSOCK_SOCKET_TYPE
    ctypes.c_int,  # protocol : INT
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('WS2_32.dll')
socket = Fiddle::Function.new(
  lib['socket'],
  [
    Fiddle::TYPE_INT,  # af : INT
    Fiddle::TYPE_INT,  # type : WINSOCK_SOCKET_TYPE
    Fiddle::TYPE_INT,  # protocol : INT
  ],
  Fiddle::TYPE_UINTPTR_T)
#[link(name = "ws2_32")]
extern "system" {
    fn socket(
        af: i32,  // INT
        type: i32,  // WINSOCK_SOCKET_TYPE
        protocol: i32  // INT
    ) -> usize;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("WS2_32.dll", SetLastError = true)]
public static extern UIntPtr socket(int af, int type, int protocol);
"@
$api = Add-Type -MemberDefinition $sig -Name 'WS2_32_socket' -Namespace Win32 -PassThru
# $api::socket(af, type, protocol)
#uselib "WS2_32.dll"
#func global socket "socket" sptr, sptr, sptr
; socket af, type, protocol   ; 戻り値は stat
; af : INT -> "sptr"
; type : WINSOCK_SOCKET_TYPE -> "sptr"
; protocol : INT -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "WS2_32.dll"
#cfunc global socket "socket" int, int, int
; res = socket(af, type, protocol)
; af : INT -> "int"
; type : WINSOCK_SOCKET_TYPE -> "int"
; protocol : INT -> "int"
; SOCKET socket(INT af, WINSOCK_SOCKET_TYPE type, INT protocol)
#uselib "WS2_32.dll"
#cfunc global socket "socket" int, int, int
; res = socket(af, type, protocol)
; af : INT -> "int"
; type : WINSOCK_SOCKET_TYPE -> "int"
; protocol : INT -> "int"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	ws2_32 = windows.NewLazySystemDLL("WS2_32.dll")
	procsocket = ws2_32.NewProc("socket")
)

// af (INT), type (WINSOCK_SOCKET_TYPE), protocol (INT)
r1, _, err := procsocket.Call(
	uintptr(af),
	uintptr(type),
	uintptr(protocol),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // SOCKET
function socket(
  af: Integer;   // INT
  type: Integer;   // WINSOCK_SOCKET_TYPE
  protocol: Integer   // INT
): NativeUInt; stdcall;
  external 'WS2_32.dll' name 'socket';
result := DllCall("WS2_32\socket"
    , "Int", af   ; INT
    , "Int", type   ; WINSOCK_SOCKET_TYPE
    , "Int", protocol   ; INT
    , "UPtr")   ; return: SOCKET
●socket(af, type, protocol) = DLL("WS2_32.dll", "int socket(int, int, int)")
# 呼び出し: socket(af, type, protocol)
# af : INT -> "int"
# type : WINSOCK_SOCKET_TYPE -> "int"
# protocol : INT -> "int"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。