Empfangen von Daten über einen Datagram-Socket
Nachdem eine WSK-Anwendung (Winsock Kernel) einen Datagrammsocket an eine lokale Transportadresse gebunden hat, kann sie Datagramme über den Socket empfangen. Eine WSK-Anwendung empfängt ein Datagramm über einen Datagrammsocket, indem sie die WskReceiveFrom-Funktion aufruft .
Das folgende Codebeispiel zeigt, wie eine WSK-Anwendung ein Datagramm über einen Datagrammsocket empfangen kann.
// 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;
}
Als Alternative zum Aufrufen der WskReceiveFrom-Funktion , um jedes Datagramm über einen Datagrammsocket zu empfangen, kann eine WSK-Anwendung die WskReceiveFromEvent-Ereignisrückruffunktion für den Socket aktivieren. Wenn eine WSK-Anwendung die WskReceiveFromEvent-Ereignisrückruffunktion für einen Datagrammsocket aktiviert, ruft das WSK-Subsystem die WskReceiveFromEvent-Ereignisrückruffunktion des Sockets auf, wenn neue Datagramme im Socket empfangen werden. Weitere Informationen zum Aktivieren der WskReceiveFromEvent-Ereignisrückruffunktion eines Datagrammsockets finden Sie unter Aktivieren und Deaktivieren von Ereignisrückruffunktionen.
Das folgende Codebeispiel zeigt, wie eine WSK-Anwendung Datagramme vom WSK-Subsystem empfangen kann, indem die WskReceiveFromEvent-Ereignisrückruffunktion eines Datagrammsockets aufgerufen wird.
// 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;
}
}
Wenn die WskReceiveFromEvent-Ereignisrückruffunktion eines Datagrammsockets nicht alle Datagramme aus der Liste der WSK_DATAGRAM_INDICATION Strukturen abruft, auf die der DataIndication-Parameter verweist, kann die Liste zur weiteren Verarbeitung beibehalten werden, indem STATUS_PENDING zurückgegeben wird. In diesem Fall muss die WSK-Anwendung die WskRelease-Funktion aufrufen, um die Liste der WSK_DATAGRAM_INDICATION Strukturen wieder an das WSK-Subsystem freizugeben, nachdem alle Datagramme aus den Strukturen in der Liste abgerufen wurden.