[ Next Article |
Previous Article |
Book Contents |
Library Home |
Legal |
Search ]
Communications Technical Reference, Volume 2
connect Subroutine
Purpose
Connects two sockets.
Library
Standard C Library (libc.a
Syntax
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/ndd_var.h>/*Needed for AF_NDD address family only*/
#include <sys/atmsock.h>/*Needed for SOCK_CONN_DGRAM socket type only*/
int connect (Socket,Name, NameLength)
int Socket;
const struct sockaddr *Name;
size_t NameLength;
Description
The connect subroutine requests a connection between two sockets. The kernel sets up the communication link between the sockets; both sockets must use the same address format and protocol.
If a connect subroutine is issued on an unbound socket, the system automatically binds the socket.
The connect subroutine performs a different action for each of the following two types of initiating sockets:
- If the initiating socket is SOCK_DGRAM, the connect subroutine establishes the peer address. The peer address identifies the socket where all datagrams are sent on subsequent send subroutines. No connections are made by this connect subroutine.
- If the initiating socket is SOCK_STREAM or SOCK_CONN_DGRAM, the connect subroutine attempts to make a connection to the socket specified by the Name parameter. Each communication space interprets the Name parameter differently. For SOCK_CONN_DGRAM socket type and ATM protocol, some of the ATM parameters may have been modified by the remote station, applications may query new values of ATM parameters using the appropriate socket options.
- In the case of a UNIX domain socket, a connect call only succeeds if the process that calls connect has read and write permissions on the socket file created by the bind call. Permissions are determined by the umask< value of the process that created the file.
Parameters
Socket |
Specifies the unique name of the socket. |
Name |
Specifies the address of target socket that will form the other end of the communication line |
NameLength |
Specifies the length of the address structure. |
Return Values
Upon successful completion, the connect subroutine returns a value of 0.
If the connect subroutine is unsuccessful, the system handler performs the following functions:
- Returns a value of -1 to the calling program.
- Moves an error code, indicating the specific error, into the errno global variable.
Error Codes
The connect subroutine is unsuccessful if any of the following errors occurs:
EBADF |
The Socket parameter is not valid. |
ENOTSOCK |
The Socket parameter refers to a file, not a socket. |
EADDRNOTAVAIL |
The specified address is not available from the local machine. |
EAFNOSUPPORT |
The addresses in the specified address family cannot be used with this socket. |
EISCONN |
The socket is already connected. |
ETIMEDOUT |
The establishment of a connection timed out before a connection was made. |
ECONNREFUSED |
The attempt to connect was rejected. |
ENETUNREACH |
No route to the network or host is present. |
EADDRINUSE |
The specified address is already in use. |
EFAULT |
The Address parameter is not in a writable part of the user address space. |
EINPROGRESS |
The socket is marked as nonblocking. The connection cannot be immediately completed. The application program can select the socket for writing during the connection process. |
EINVAL |
The specified path name contains a character with the high-order bit set. |
ENETDOWN |
The specified physical network is down. |
ENOSPC |
There is no space left on a device or system table. |
ENOTCONN |
The socket could not be connected. |
Examples
The following program fragment illustrates the use of the connect subroutine by a client to initiate a connection to a server's socket.
struct sockaddr_un server;
.
.
.
connect(s,(struct sockaddr*)&server, sun_len(&server));
Implementation Specifics
The connect subroutine is part of Base Operating System (BOS) Runtime.
The socket applications can be compiled with COMPAT_43 defined. This will make the sockaddr structure BSD 4.3 compatible. For more details refer to socket.h.
If the environment variable SOCKS5C_CONFIG is set, the connect subroutine may attempt to make the requested connection through a SOCKS server.
Related Information
The
accept subroutine,
bind subroutine,
getsockname subroutine,
send subroutine,
socket, subroutine,
socks5tcp_connect subroutine.
Initiating UNIX Stream Connections Example Program,
Sockets Overview,
and Understanding Socket Connections in AIX Version 4.3 Communications Programming Concepts.
[ Next Article |
Previous Article |
Book Contents |
Library Home |
Legal |
Search ]