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

connect

関数
指定アドレスへのソケット接続を確立する。
DLLWS2_32.dll呼出規約winapiSetLastErrorあり対応OSWindows Vista 以降

シグネチャ

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

INT connect(
    SOCKET s,
    const SOCKADDR* name,
    INT namelen
);

パラメーター

名前方向
sSOCKETin
nameSOCKADDR*in
namelenINTin

戻り値の型: INT

各言語での呼び出し定義

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

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

connect = ctypes.windll.ws2_32.connect
connect.restype = ctypes.c_int
connect.argtypes = [
    ctypes.c_size_t,  # s : SOCKET
    ctypes.c_void_p,  # name : SOCKADDR*
    ctypes.c_int,  # namelen : INT
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('WS2_32.dll')
connect = Fiddle::Function.new(
  lib['connect'],
  [
    Fiddle::TYPE_UINTPTR_T,  # s : SOCKET
    Fiddle::TYPE_VOIDP,  # name : SOCKADDR*
    Fiddle::TYPE_INT,  # namelen : INT
  ],
  Fiddle::TYPE_INT)
#[link(name = "ws2_32")]
extern "system" {
    fn connect(
        s: usize,  // SOCKET
        name: *const SOCKADDR,  // SOCKADDR*
        namelen: i32  // INT
    ) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("WS2_32.dll", SetLastError = true)]
public static extern int connect(UIntPtr s, IntPtr name, int namelen);
"@
$api = Add-Type -MemberDefinition $sig -Name 'WS2_32_connect' -Namespace Win32 -PassThru
# $api::connect(s, name, namelen)
#uselib "WS2_32.dll"
#func global connect "connect" sptr, sptr, sptr
; connect s, varptr(name), namelen   ; 戻り値は stat
; s : SOCKET -> "sptr"
; name : SOCKADDR* -> "sptr"
; namelen : INT -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "WS2_32.dll"
#cfunc global connect "connect" sptr, var, int
; res = connect(s, name, namelen)
; s : SOCKET -> "sptr"
; name : SOCKADDR* -> "var"
; namelen : INT -> "int"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; INT connect(SOCKET s, SOCKADDR* name, INT namelen)
#uselib "WS2_32.dll"
#cfunc global connect "connect" intptr, var, int
; res = connect(s, name, namelen)
; s : SOCKET -> "intptr"
; name : SOCKADDR* -> "var"
; namelen : INT -> "int"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	ws2_32 = windows.NewLazySystemDLL("WS2_32.dll")
	procconnect = ws2_32.NewProc("connect")
)

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