透過資料包通訊端傳送資料
在 Winsock 核心 (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。