透過Connection-Oriented通訊端傳送資料
在 Winsock 核心 (WSK) 應用程式將連線導向通訊端連線到遠端傳輸位址之後,就可以透過通訊端傳送資料。 WSK 應用程式也可以透過接聽通訊端上接受的連接導向通訊端傳送資料。 WSK 應用程式會呼叫 WskSend 函式,透過連線導向通訊端傳送資料。
下列程式碼範例示範 WSK 應用程式如何透過連線導向通訊端傳送資料。
// Prototype for the send IoCompletion routine
NTSTATUS
SendComplete(
PDEVICE_OBJECT DeviceObject,
PIRP Irp,
PVOID Context
);
// Function to send data
NTSTATUS
SendData(
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,
SendComplete,
DataBuffer, // Use the data buffer for the context
TRUE,
TRUE,
TRUE
);
// Initiate the send operation on the socket
Status =
Dispatch->WskSend(
Socket,
DataBuffer,
0, // No flags
Irp
);
// Return the status of the call to WskSend()
return Status;
}
// Send IoCompletion routine
NTSTATUS
SendComplete(
PDEVICE_OBJECT DeviceObject,
PIRP Irp,
PVOID Context
)
{
UNREFERENCED_PARAMETER(DeviceObject);
PWSK_BUF DataBuffer;
ULONG ByteCount;
// Check the result of the send operation
if (Irp->IoStatus.Status == STATUS_SUCCESS)
{
// Get the pointer to the data buffer
DataBuffer = (PWSK_BUF)Context;
// Get the number of bytes sent
ByteCount = (ULONG)(Irp->IoStatus.Information);
// Re-use or free the data 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;
}