Отправка данных через сокет 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;
}