Nota:
El acceso a esta página requiere autorización. Puede intentar iniciar sesión o cambiar directorios.
El acceso a esta página requiere autorización. Puede intentar cambiar los directorios.
Después de que una aplicación de Winsock Kernel (WSK) se haya conectado correctamente al subsistema WSK, puede crear sockets que se pueden usar para las operaciones de E/S de red. Una aplicación WSK crea sockets mediante una llamada a la función WskSocket . El miembro WskSocket apunta a la función WskSocket de la estructura de WSK_PROVIDER_DISPATCH que devolvió el subsistema WSK durante los datos adjuntos.
Una aplicación WSK debe especificar qué categoría de socket WSK está creando cada vez que crea un nuevo socket. Para obtener más información sobre las categorías de socket de WSK, vea Categorías de sockets de kernel de Winsock.
Una aplicación WSK también debe especificar la familia de direcciones, el tipo de socket y el protocolo siempre que cree un nuevo socket. Para obtener más información sobre las familias de direcciones compatibles con WSK, consulte Familias de direcciones de WSK.
Al crear un nuevo socket, una aplicación WSK debe proporcionar un valor de contexto de socket y un puntero a una estructura de tabla de distribución de cliente si la aplicación habilitará cualquier función de devolución de llamada de eventos en el socket. Para obtener más información sobre cómo habilitar funciones de devolución de llamada de eventos en un socket, consulte Habilitación y deshabilitación de funciones de devolución de llamada de eventos.
Si el socket se crea correctamente, el campo IoStatus.Information del IRP contiene un puntero a una estructura de objetos de socket ( WSK_SOCKET) para el nuevo socket. Para obtener más información sobre el uso de IRP con funciones WSK, consulte Uso de IRP con funciones del kernel de Winsock.
En el ejemplo de código siguiente se muestra cómo una aplicación WSK puede crear un socket de escucha.
// Context structure for each socket
typedef struct _WSK_APP_SOCKET_CONTEXT {
PWSK_SOCKET Socket;
.
. // Other application-specific members
.
} WSK_APP_SOCKET_CONTEXT, *PWSK_APP_SOCKET_CONTEXT;
// Prototype for the socket creation IoCompletion routine
NTSTATUS
CreateListeningSocketComplete(
PDEVICE_OBJECT DeviceObject,
PIRP Irp,
PVOID Context
);
// Function to create a new listening socket
NTSTATUS
CreateListeningSocket(
PWSK_PROVIDER_NPI WskProviderNpi,
PWSK_APP_SOCKET_CONTEXT SocketContext,
PWSK_CLIENT_LISTEN_DISPATCH Dispatch,
)
{
PIRP Irp;
NTSTATUS Status;
// 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,
CreateListeningSocketComplete,
SocketContext,
TRUE,
TRUE,
TRUE
);
// Initiate the creation of the socket
Status =
WskProviderNpi->Dispatch->
WskSocket(
WskProviderNpi->Client,
AF_INET,
SOCK_STREAM,
IPPROTO_TCP,
WSK_FLAG_LISTEN_SOCKET,
SocketContext,
Dispatch,
NULL,
NULL,
NULL,
Irp
);
// Return the status of the call to WskSocket()
return Status;
}
// Socket creation IoCompletion routine
NTSTATUS
CreateListeningSocketComplete(
PDEVICE_OBJECT DeviceObject,
PIRP Irp,
PVOID Context
)
{
UNREFERENCED_PARAMETER(DeviceObject);
PWSK_APP_SOCKET_CONTEXT SocketContext;
// Check the result of the socket creation
if (Irp->IoStatus.Status == STATUS_SUCCESS)
{
// Get the pointer to the socket context
SocketContext =
(PWSK_APP_SOCKET_CONTEXT)Context;
// Save the socket object for the new socket
SocketContext->Socket =
(PWSK_SOCKET)(Irp->IoStatus.Information);
// Set any socket options for the new socket
...
// Enable any event callback functions on the new socket
...
// Perform any other initializations
...
}
// 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;
}
En el caso de los sockets orientados a la conexión, una aplicación WSK puede llamar a la función WskSocketConnect para crear, enlazar y conectar un socket en una sola llamada de función.