共用方式為


透過資料包通訊端接收資料

在 Winsock 核心 (WSK) 應用程式將資料包通訊端系結至本機傳輸位址之後,它可以透過通訊端接收資料包。 WSK 應用程式會藉由呼叫 WskReceiveFrom 函式,透過資料包通訊端接收資料包。

下列程式碼範例示範 WSK 應用程式如何透過資料包通訊端接收資料包。

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

// Function to receive a datagram
NTSTATUS
  ReceiveDatagram(
    PWSK_SOCKET Socket,
    PWSK_BUF DatagramBuffer
    )
{
  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,
    ReceiveDatagramComplete,
    DatagramBuffer,  // Use the datagram buffer for the context
    TRUE,
    TRUE,
    TRUE
    );

  // Initiate the receive operation on the socket
  Status =
    Dispatch->WskReceiveFrom(
      Socket,
      DatagramBuffer,
      0,  // No flags are specified
      NULL,  // Not interested in the remote address
      NULL,  // Not interested in any associated control information
      NULL,
      NULL,
      Irp
      );

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

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

  PWSK_BUF DataBuffer;
  ULONG ByteCount;

  // Check the result of the receive operation
  if (Irp->IoStatus.Status == STATUS_SUCCESS)
  {
    // Get the pointer to the data buffer
    DataBuffer = (PWSK_BUF)Context;
 
    // Get the number of bytes received
    ByteCount = (ULONG)(Irp->IoStatus.Information);

    // Process the received datagram
    ...
  }

  // 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;
}

除了呼叫 WskReceiveFrom 函 式以透過資料包通訊端接收每個資料包,WSK 應用程式可以在通訊端上啟用 WskReceiveFromEvent 事件回呼函式。 如果 WSK 應用程式在資料包通訊端上啟用 WskReceiveFromEvent 事件回呼函式,WSK 子系統會在通訊端上收到新的資料包時呼叫通訊端的 WskReceiveFromEvent 事件回呼函式。 如需啟用資料包通訊端 WskReceiveFromEvent 事件回呼函式的詳細資訊,請參閱 啟用和停用事件回呼函式

下列程式碼範例示範 WSK 應用程式如何藉由呼叫資料包通訊端的 WskReceiveFromEvent 事件回呼函式,接收 WSK 子系統的資料包。

// A datagram socket's WskReceiveFromEvent
// event callback function
NTSTATUS WSKAPI
  WskReceiveFromEvent(
    PVOID SocketContext,
    ULONG Flags,
    PWSK_DATAGRAM_INDICATION DataIndication
    )
{
  // Check for a valid data indication
  if (DataIndication != NULL)
  {
    // Loop through the list of data indication structures
    while (DataIndication != NULL)
    {
      // Process the data in the data indication structure
      ...

      // Move to the next data indication structure
      DataIndication = DataIndication->Next;
    }

    // Return status indicating the datagrams were received
    return STATUS_SUCCESS;
  }

  // Error
  else
  {
    // Close the socket
    ...

    // Return success since no datagrams were indicated
    return STATUS_SUCCESS;
  }
}

如果資料包通訊端的WskReceiveFromEvent事件回呼函式未從DataIndication參數所指向的WSK_DATAGRAM_INDICATION結構清單中擷取所有資料包,它可以傳回 STATUS_PENDING 來保留清單以供進一步處理。 在此情況下,WSK 應用程式必須呼叫 WskRelease 函式,以在完成從清單中的結構擷取所有資料包之後,將WSK_DATAGRAM_INDICATION結構的清單釋放回 WSK 子系統。