在 Winsock Kernel (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 應用程式如何藉由呼叫 datagram socket 的 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 子系統。