共用方式為


透過Connection-Oriented通訊端接收資料

在 Winsock 核心 (WSK) 應用程式將連線導向通訊端連線到遠端傳輸位址之後,就可以透過通訊端接收資料。 WSK 應用程式也可以透過接聽通訊端上接受的連接導向通訊端接收資料。 WSK 應用程式藉由呼叫 WskReceive 函式,透過連線導向通訊端接收資料。

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

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

// Function to receive data
NTSTATUS
  ReceiveData(
    PWSK_SOCKET Socket,
    PWSK_BUF DataBuffer
    )
{
  PWSK_PROVIDER_CONNECTION_DISPATCH Dispatch;
  PIRP Irp;
  NTSTATUS Status;

  // Get pointer to the provider dispatch structure
  Dispatch =
    (PWSK_PROVIDER_CONNECTION_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,
    ReceiveComplete,
    DataBuffer,  // Use the data buffer for the context
    TRUE,
    TRUE,
    TRUE
    );

  // Initiate the receive operation on the socket
  Status =
    Dispatch->WskReceive(
      Socket,
      DataBuffer,
      0,  // No flags are specified
      Irp
      );

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

// Receive IoCompletion routine
NTSTATUS
  ReceiveComplete(
    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 data
    ...
  }

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

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

下列程式碼範例示範 WSK 應用程式如何透過呼叫連接導向通訊端的 WskReceiveEvent 事件回呼函式的 WSK 子系統接收資料。

// A connection-oriented socket's WskReceiveEvent
// event callback function
NTSTATUS WSKAPI
  WskReceiveEvent(
    PVOID SocketContext,
    ULONG Flags,
    PWSK_DATA_INDICATION DataIndication,
    SIZE_T BytesIndicated,
    SIZE_T *BytesAccepted
    )
{
  // 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 data was received
    return STATUS_SUCCESS;
  }

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

    // Return success since no data was indicated
    return STATUS_SUCCESS;
  }
}

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

如果連接導向通訊端的 WskReceiveEvent 事件回呼函式只接受所接收資料位元組總數的一部分,則必須將 BytesAccepted 參數指向的變數設定為實際接受的資料位元組數目。 不過,如果通訊端的 WskReceiveEvent 事件回呼函式接受所有接收的資料,則不需要設定 BytesAccepted 參數指向的變數。