ホーム › Networking.WinSock › recv
recv
関数接続ソケットからデータを受信する。
シグネチャ
// WS2_32.dll
#include <windows.h>
INT recv(
SOCKET s,
LPSTR buf,
INT len,
SEND_RECV_FLAGS flags
);パラメーター
| 名前 | 型 | 方向 |
|---|---|---|
| s | SOCKET | in |
| buf | LPSTR | out |
| len | INT | in |
| flags | SEND_RECV_FLAGS | in |
戻り値の型: INT
各言語での呼び出し定義
// WS2_32.dll
#include <windows.h>
INT recv(
SOCKET s,
LPSTR buf,
INT len,
SEND_RECV_FLAGS flags
);[DllImport("WS2_32.dll", SetLastError = true, ExactSpelling = true)]
static extern int recv(
UIntPtr s, // SOCKET
[MarshalAs(UnmanagedType.LPStr)] System.Text.StringBuilder buf, // LPSTR out
int len, // INT
int flags // SEND_RECV_FLAGS
);<DllImport("WS2_32.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function recv(
s As UIntPtr, ' SOCKET
<MarshalAs(UnmanagedType.LPStr)> buf As System.Text.StringBuilder, ' LPSTR out
len As Integer, ' INT
flags As Integer ' SEND_RECV_FLAGS
) As Integer
End Function' s : SOCKET
' buf : LPSTR out
' len : INT
' flags : SEND_RECV_FLAGS
Declare PtrSafe Function recv Lib "ws2_32" ( _
ByVal s As LongPtr, _
ByVal buf As String, _
ByVal len As Long, _
ByVal flags As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。import ctypes
from ctypes import wintypes
recv = ctypes.windll.ws2_32.recv
recv.restype = ctypes.c_int
recv.argtypes = [
ctypes.c_size_t, # s : SOCKET
wintypes.LPSTR, # buf : LPSTR out
ctypes.c_int, # len : INT
ctypes.c_int, # flags : SEND_RECV_FLAGS
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('WS2_32.dll')
recv = Fiddle::Function.new(
lib['recv'],
[
Fiddle::TYPE_UINTPTR_T, # s : SOCKET
Fiddle::TYPE_VOIDP, # buf : LPSTR out
Fiddle::TYPE_INT, # len : INT
Fiddle::TYPE_INT, # flags : SEND_RECV_FLAGS
],
Fiddle::TYPE_INT)#[link(name = "ws2_32")]
extern "system" {
fn recv(
s: usize, // SOCKET
buf: *mut u8, // LPSTR out
len: i32, // INT
flags: i32 // SEND_RECV_FLAGS
) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.$sig = @"
[DllImport("WS2_32.dll", SetLastError = true)]
public static extern int recv(UIntPtr s, [MarshalAs(UnmanagedType.LPStr)] System.Text.StringBuilder buf, int len, int flags);
"@
$api = Add-Type -MemberDefinition $sig -Name 'WS2_32_recv' -Namespace Win32 -PassThru
# $api::recv(s, buf, len, flags)#uselib "WS2_32.dll"
#func global recv "recv" sptr, sptr, sptr, sptr
; recv s, varptr(buf), len, flags ; 戻り値は stat
; s : SOCKET -> "sptr"
; buf : LPSTR out -> "sptr"
; len : INT -> "sptr"
; flags : SEND_RECV_FLAGS -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。出力引数:
#uselib "WS2_32.dll" #cfunc global recv "recv" sptr, var, int, int ; res = recv(s, buf, len, flags) ; s : SOCKET -> "sptr" ; buf : LPSTR out -> "var" ; len : INT -> "int" ; flags : SEND_RECV_FLAGS -> "int" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。#uselib "WS2_32.dll" #cfunc global recv "recv" sptr, sptr, int, int ; res = recv(s, varptr(buf), len, flags) ; s : SOCKET -> "sptr" ; buf : LPSTR out -> "sptr" ; len : INT -> "int" ; flags : SEND_RECV_FLAGS -> "int" ; ※出力/バッファ引数はポインタ方式(token=sptr / 呼び出しは varptr(変数))。
出力引数:
; INT recv(SOCKET s, LPSTR buf, INT len, SEND_RECV_FLAGS flags) #uselib "WS2_32.dll" #cfunc global recv "recv" intptr, var, int, int ; res = recv(s, buf, len, flags) ; s : SOCKET -> "intptr" ; buf : LPSTR out -> "var" ; len : INT -> "int" ; flags : SEND_RECV_FLAGS -> "int" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。; INT recv(SOCKET s, LPSTR buf, INT len, SEND_RECV_FLAGS flags) #uselib "WS2_32.dll" #cfunc global recv "recv" intptr, intptr, int, int ; res = recv(s, varptr(buf), len, flags) ; s : SOCKET -> "intptr" ; buf : LPSTR out -> "intptr" ; len : INT -> "int" ; flags : SEND_RECV_FLAGS -> "int" ; ※出力/バッファ引数はポインタ方式(token=intptr / 呼び出しは varptr(変数))。
import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
ws2_32 = windows.NewLazySystemDLL("WS2_32.dll")
procrecv = ws2_32.NewProc("recv")
)
// s (SOCKET), buf (LPSTR out), len (INT), flags (SEND_RECV_FLAGS)
r1, _, err := procrecv.Call(
uintptr(s),
uintptr(buf),
uintptr(len),
uintptr(flags),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // INTfunction recv(
s: NativeUInt; // SOCKET
buf: PAnsiChar; // LPSTR out
len: Integer; // INT
flags: Integer // SEND_RECV_FLAGS
): Integer; stdcall;
external 'WS2_32.dll' name 'recv';result := DllCall("WS2_32\recv"
, "UPtr", s ; SOCKET
, "Ptr", buf ; LPSTR out
, "Int", len ; INT
, "Int", flags ; SEND_RECV_FLAGS
, "Int") ; return: INT●recv(s, buf, len, flags) = DLL("WS2_32.dll", "int recv(int, char*, int, int)")
# 呼び出し: recv(s, buf, len, flags)
# s : SOCKET -> "int"
# buf : LPSTR out -> "char*"
# len : INT -> "int"
# flags : SEND_RECV_FLAGS -> "int"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。