CONNECT(3SOCKET) | Sockets Library Functions | CONNECT(3SOCKET) |
cc [ flag ... ] file ... -lsocket -lnsl [ library ... ] #include <sys/types.h> #include <sys/socket.h> int connect(int s, const struct sockaddr *name, int namelen);
Applications can obtain the state of this connection attempt by polling the socket's file descriptor for POLLOUT. The event ports facility is the preferred means of polling on the file descriptor, see port_create(3C) and port_get(3C) for more information on event ports; however, applications may also use traditional portable routines like poll(2) and select(3C).
When an asynchronous connection has completed, the application must call getsockopt(3SOCKET) using the macro SOL_SOCKET as the level argument and the macro SO_ERROR as the value of the option argument. If the value of the SO_ERROR socket option is zero, then the connect was successfully established. Otherwise, the connection could not be established and the value is the corresponding error code that would be commonly found in errno.
Even when a socket is in non-blocking mode, a call to connect may fail synchronously. If any error other EINPROGRESS or EINTR occurs, then there is no need for the application to poll for asynchronous completion. Similarly, if a call to connect returns successfully, then the socket connection will be established and there is no need to poll for completion.
The following sample C program shows how to create and connect to a remote host using TCP. The program should be compiled and linked against libnsl and libsocket. For example, if the contents of this example where in a file called example.c, one would run cc example.c -lnsl -lsocket.
#include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <inttypes.h> #include <stdio.h> #include <strings.h> #include <stdlib.h> #include <errno.h> #include <port.h> #include <unistd.h> #include <assert.h> int main(int argc, char *argv[]) { char *eptr; long port; int sock, ret, eport; struct sockaddr_in6 sin6; if (argc != 3) { fprintf(stderr, "connect: <IP> <port>\n"); return (1); } bzero(&sin6, sizeof (struct sockaddr_in6)); sin6.sin6_family = AF_INET6; /* * Try to parse as an IPv6 address and then try v4. */ ret = inet_pton(AF_INET6, argv[1], &sin6.sin6_addr); if (ret == -1) { perror("inet_pton"); return (1); } else if (ret == 0) { struct in_addr v4; ret = inet_pton(AF_INET, argv[1], &v4); if (ret == -1) { perror("inet_pton"); return (1); } else if (ret == 0) { fprintf(stderr, "connect: %s is not a valid " "IPv4 or IPv6 address\n", argv[1]); return (1); } /* N.B. Not a portable macro */ IN6_INADDR_TO_V4MAPPED(&v4, &sin6.sin6_addr); } errno = 0; port = strtol(argv[2], &eptr, 10); if (errno != 0 || *eptr != '\0') { fprintf(stderr, "failed to parse port %s\n", argv[2]); return (1); } if (port <= 0 || port > UINT16_MAX) { fprintf(stderr, "invalid port: %ld\n", port); return (1); } sin6.sin6_port = htons(port); sock = socket(AF_INET6, SOCK_STREAM | SOCK_NONBLOCK, 0); if (sock < 0) { perror("socket"); return (1); } eport = port_create(); if (eport < 0) { perror("port_create"); (void) close(sock); return (1); } ret = connect(sock, (struct sockaddr *)&sin6, sizeof (struct sockaddr_in6)); if (ret != 0 && errno != EINPROGRESS && errno != EINTR) { perror("connect"); (void) close(sock); (void) close(eport); return (1); } if (ret != 0) { port_event_t pe; int err; socklen_t sz = sizeof (err); if (port_associate(eport, PORT_SOURCE_FD, sock, POLLOUT, NULL) != 0) { perror("port_associate"); (void) close(sock); (void) close(eport); return (1); } if (port_get(eport, &pe, NULL) != 0) { perror("port_get"); (void) close(sock); (void) close(eport); return (1); } assert(pe.portev_source == PORT_SOURCE_FD); assert(pe.portev_object == (uintptr_t)sock); if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &err, &sz) != 0) { perror("getsockopt"); (void) close(sock); (void) close(eport); return (1); } if (err != 0) { /* Asynch connect failed */ fprintf(stderr, "asynchronous connect: %s\n", strerror(err)); (void) close(sock); (void) close(eport); return (1); } } /* Read and write to the socket and then clean up */ return (0); }
EACCES
EADDRINUSE
EADDRNOTAVAIL
EAFNOSUPPORT
EALREADY
EBADF
ECONNREFUSED
EINPROGRESS
EINTR
EINVAL
EIO
EISCONN
ELOOP
ENETUNREACH
EHOSTUNREACH
ENOENT
ENOENT
ENOSR
ENXIO
ETIMEDOUT
EWOULDBLOCK
The following errors are specific to connecting names in the UNIX domain. These errors might not apply in future versions of the UNIX IPC domain.
ENOTDIR
ENOTSOCK
ENOTSOCK
EPROTOTYPE
ATTRIBUTE TYPE | ATTRIBUTE VALUE |
MT-Level | Safe |
November 25, 2014 | OmniOS |