connect
Establishes a connection to a specified remote host and port.
Syntax
int connect(
SOCKET s,
const struct sockaddr *name,
int namelen
);
Parameters
Parameter | Description |
---|---|
s |
A descriptor identifying an unconnected socket. |
name |
A pointer to a sockaddr structure that specifies the destination address and port for the connection. |
namelen |
The size, in bytes, of the sockaddr structure pointed to by the name parameter. |
Return Value
If the connection is successfully established, connect
returns zero. Otherwise, it returns SOCKET_ERROR
, and a specific error code can be retrieved by calling WSAGetLastError
.
Remarks
The connect
function is used to establish a connection on a socket. For connection-oriented protocols, this is the explicit connection setup phase. For datagram sockets, this call is a permissible but not required operation. If used with a datagram socket, it specifies the default destination for send
operations and the default destination for recv
operations.
When a connect
call is made on a connectionless socket (such as SOCK_DGRAM
), the local transport provider is instructed to deliver datagrams only to the specified foreign host and port. Any datagrams sent to a different destination will be returned with a WSAECONNRESET
error.
If the socket is non-blocking and the connection cannot be established immediately, connect
will return WSAEWOULDBLOCK
. The application can then use WSAAsyncSelect
or WSAEventSelect
to be notified when the connection is established (represented by the FD_CONNECT
network event).
A connect
call on a non-blocking socket will succeed if the connection can be established immediately. Otherwise, the call returns WSAEWOULDBLOCK
.
Examples
The following example demonstrates how to use the connect
function:
// Include Winsock header
#include <winsock2.h>
#include <ws2tcpip.h>
#pragma comment(lib, "Ws2_32.lib")
int main() {
WSADATA wsaData;
int iResult;
// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != 0) {
// Handle error
printf("WSAStartup failed: %d\n", iResult);
return 1;
}
SOCKET ConnectSocket = INVALID_SOCKET;
struct sockaddr_in clientService;
// Create a SOCKET for connecting to server
ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (ConnectSocket == INVALID_SOCKET) {
// Handle error
printf("Error at socket(): %ld\n", WSAGetLastError());
WSACleanup();
return 1;
}
//----------------------------------------------------------------------------------
// The sockaddr_in structure specifies the address family,
// IP address, and port for the server to connect to.
//----------------------------------------------------------------------------------
clientService.sin_family = AF_INET;
clientService.sin_addr.s_addr = inet_addr("127.0.0.1"); // Replace with server IP
clientService.sin_port = htons(8080); // Replace with server port
//----------------------------------------------------------------------------------
// Connect to server.
//----------------------------------------------------------------------------------
iResult = connect(ConnectSocket, (SOCKADDR*)&clientService, sizeof(clientService));
if (iResult == SOCKET_ERROR) {
// Handle error
printf("connect failed with error: %d\n", WSAGetLastError());
closesocket(ConnectSocket);
WSACleanup();
return 1;
}
// Connection successful
printf("Connected to server!\n");
// Send and receive data here...
// Close the socket
closesocket(ConnectSocket);
WSACleanup();
return 0;
}