다음을 통해 공유


Datagram 소켓을 통해 데이터 보내기

Winsock Kernel(WSK) 애플리케이션이 데이터그램 소켓을 로컬 전송 주소에 바인딩한 후에는 소켓을 통해 데이터그램을 보낼 수 있습니다. WSK 애플리케이션은 WskSendTo 함수를 호출하여 데이터그램 소켓을 통해 데이터그램을 보냅니다.

다음 코드 예제에서는 WSK 애플리케이션이 데이터그램 소켓을 통해 데이터그램을 보낼 수 있는 방법을 보여 줍니다.

// Prototype for the send datagram IoCompletion routine
NTSTATUS
  SendDatagramComplete(
    PDEVICE_OBJECT DeviceObject,
    PIRP Irp,
    PVOID Context
    );

// Function to send a datagram
NTSTATUS
  SendDatagram(
    PWSK_SOCKET Socket,
    PWSK_BUF DatagramBuffer,
    PSOCKADDR RemoteAddress
    )
{
  PWSK_PROVIDER_DATAGRAM_DISPATCH Dispatch;
  PIRP Irp;
  NTSTATUS Status;

  // Get pointer to the provider dispatch structure
  Dispatch =
    (PWSK_PROVIDER_DATAGRAM_DISPATCH)(Socket->Dispatch);

  // Allocate an IRP
  Irp =
    IoAllocateIrp(
      1,
      FALSE
      );

  // Check result
  if (!Irp)
  {
    // Return error
    return STATUS_INSUFFICIENT_RESOURCES;
  }

  // Set the completion routine for the IRP
  IoSetCompletionRoutine(
    Irp,
    SendDatagramComplete,
    DatagramBuffer,  // Use the datagram buffer for the context
    TRUE,
    TRUE,
    TRUE
    );

  // Initiate the send operation on the socket
  Status =
    Dispatch->WskSendTo(
      Socket,
      DatagramBuffer,
      0,  // No flags
      RemoteAddress,
      0,
      NULL,  // No associated control info
      Irp
      );

  // Return the status of the call to WskSendTo()
  return Status;
}

// Send datagram IoCompletion routine
NTSTATUS
  SendDatagramComplete(
    PDEVICE_OBJECT DeviceObject,
    PIRP Irp,
    PVOID Context
    )
{
  UNREFERENCED_PARAMETER(DeviceObject);

  PWSK_BUF DatagramBuffer;
  ULONG ByteCount;

  // Check the result of the send operation
  if (Irp->IoStatus.Status == STATUS_SUCCESS)
  {
    // Get the pointer to the datagram buffer
    DatagramBuffer = (PWSK_BUF)Context;

    // Get the number of bytes sent
    ByteCount = (ULONG)(Irp->IoStatus.Information);

    // Re-use or free the datagram buffer
    ...
  }

  // Error status
  else
  {
    // Handle error
    ...
  }

  // Free the IRP
  IoFreeIrp(Irp);

  // Always return STATUS_MORE_PROCESSING_REQUIRED to
  // terminate the completion processing of the IRP.
  return STATUS_MORE_PROCESSING_REQUIRED;
}

WSK 애플리케이션이 데이터그램 소켓에 대해 고정 원격 전송 주소 또는 고정 대상 전송 주소를 설정한 경우 WskSendTo 함수에 전달된 RemoteAddress 매개 변수는 선택 사항이며 NULL일 수 있습니다. NULL이면 데이터그램이 고정 원격 전송 주소 또는 고정 대상 전송 주소로 전송됩니다. NULL이 아닌 경우 데이터그램이 지정된 원격 전송 주소로 전송됩니다.

데이터그램 소켓에 대한 고정 원격 전송 주소를 설정하는 방법에 대한 자세한 내용은 SIO_WSK_SET_REMOTE_ADDRESS.

데이터그램 소켓의 고정 대상 전송 주소를 설정하는 방법에 대한 자세한 내용은 SIO_WSK_SET_SENDTO_ADDRESS.