Establishing a Connection with a Destination
After a Winsock Kernel (WSK) application has bound a connection-oriented socket to a local transport address, it can connect the socket to a remote transport address in order to establish a connection with the remote system. A WSK application must connect a connection-oriented socket to a remote transport address before it can send or receive data over the socket.
A WSK application connects a socket to a remote transport address by calling the WskConnect function. The WskConnect function is pointed to by the WskConnect member of the socket's provider dispatch structure. A socket's provider dispatch structure is pointed to by the Dispatch member of the socket object structure ( WSK_SOCKET) that was returned by the WSK subsystem during the creation of the socket.
The following code example shows how a WSK application can connect a connection-oriented socket to a remote transport address.
// Prototype for the connect IoCompletion routine
NTSTATUS
ConnectComplete(
PDEVICE_OBJECT DeviceObject,
PIRP Irp,
PVOID Context
);
// Function to connect a socket to a remote transport address
NTSTATUS
ConnectSocket(
PWSK_SOCKET Socket,
PSOCKADDR RemoteAddress
)
{
PWSK_PROVIDER_CONNECTION_DISPATCH Dispatch;
PIRP Irp;
NTSTATUS Status;
// Get pointer to the socket's 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,
ConnectComplete,
Socket, // Use the socket object for the context
TRUE,
TRUE,
TRUE
);
// Initiate the connect operation on the socket
Status =
Dispatch->WskConnect(
Socket,
RemoteAddress,
0, // No flags
Irp
);
// Return the status of the call to WskConnect()
return Status;
}
// Connect IoCompletion routine
NTSTATUS
ConnectComplete(
PDEVICE_OBJECT DeviceObject,
PIRP Irp,
PVOID Context
)
{
UNREFERENCED_PARAMETER(DeviceObject);
PWSK_SOCKET Socket;
// Check the result of the connect operation
if (Irp->IoStatus.Status == STATUS_SUCCESS)
{
// Get the socket object from the context
Socket = (PWSK_SOCKET)Context;
// Perform the next operation on the socket
...
}
// 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;
}
A WSK application can call the WskSocketConnect function to create, bind, and connect a connection-oriented socket in a single function call.
Feedback
https://aka.ms/ContentUserFeedback.
Coming soon: Throughout 2024 we will be phasing out GitHub Issues as the feedback mechanism for content and replacing it with a new feedback system. For more information see:Submit and view feedback for