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

send

関数
接続ソケットへデータを送信する。
DLLWS2_32.dll呼出規約winapiSetLastErrorあり対応OSWindows 8.1 以降

シグネチャ

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

INT send(
    SOCKET s,
    LPCSTR buf,
    INT len,
    SEND_RECV_FLAGS flags
);

パラメーター

名前方向
sSOCKETin
bufLPCSTRin
lenINTin
flagsSEND_RECV_FLAGSin

戻り値の型: INT

各言語での呼び出し定義

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

INT send(
    SOCKET s,
    LPCSTR buf,
    INT len,
    SEND_RECV_FLAGS flags
);
[DllImport("WS2_32.dll", SetLastError = true, ExactSpelling = true)]
static extern int send(
    UIntPtr s,   // SOCKET
    [MarshalAs(UnmanagedType.LPStr)] string buf,   // LPCSTR
    int len,   // INT
    int flags   // SEND_RECV_FLAGS
);
<DllImport("WS2_32.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function send(
    s As UIntPtr,   ' SOCKET
    <MarshalAs(UnmanagedType.LPStr)> buf As String,   ' LPCSTR
    len As Integer,   ' INT
    flags As Integer   ' SEND_RECV_FLAGS
) As Integer
End Function
' s : SOCKET
' buf : LPCSTR
' len : INT
' flags : SEND_RECV_FLAGS
Declare PtrSafe Function send 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

send = ctypes.windll.ws2_32.send
send.restype = ctypes.c_int
send.argtypes = [
    ctypes.c_size_t,  # s : SOCKET
    wintypes.LPCSTR,  # buf : LPCSTR
    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')
send = Fiddle::Function.new(
  lib['send'],
  [
    Fiddle::TYPE_UINTPTR_T,  # s : SOCKET
    Fiddle::TYPE_VOIDP,  # buf : LPCSTR
    Fiddle::TYPE_INT,  # len : INT
    Fiddle::TYPE_INT,  # flags : SEND_RECV_FLAGS
  ],
  Fiddle::TYPE_INT)
#[link(name = "ws2_32")]
extern "system" {
    fn send(
        s: usize,  // SOCKET
        buf: *const u8,  // LPCSTR
        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 send(UIntPtr s, [MarshalAs(UnmanagedType.LPStr)] string buf, int len, int flags);
"@
$api = Add-Type -MemberDefinition $sig -Name 'WS2_32_send' -Namespace Win32 -PassThru
# $api::send(s, buf, len, flags)
#uselib "WS2_32.dll"
#func global send "send" sptr, sptr, sptr, sptr
; send s, buf, len, flags   ; 戻り値は stat
; s : SOCKET -> "sptr"
; buf : LPCSTR -> "sptr"
; len : INT -> "sptr"
; flags : SEND_RECV_FLAGS -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "WS2_32.dll"
#cfunc global send "send" sptr, str, int, int
; res = send(s, buf, len, flags)
; s : SOCKET -> "sptr"
; buf : LPCSTR -> "str"
; len : INT -> "int"
; flags : SEND_RECV_FLAGS -> "int"
; INT send(SOCKET s, LPCSTR buf, INT len, SEND_RECV_FLAGS flags)
#uselib "WS2_32.dll"
#cfunc global send "send" intptr, str, int, int
; res = send(s, buf, len, flags)
; s : SOCKET -> "intptr"
; buf : LPCSTR -> "str"
; len : INT -> "int"
; flags : SEND_RECV_FLAGS -> "int"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	ws2_32 = windows.NewLazySystemDLL("WS2_32.dll")
	procsend = ws2_32.NewProc("send")
)

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