connect(2) CLIX connect(2)
NAME
connect - Initiates a connection on a socket
LIBRARY
Berkeley Software Distribution Library (libbsd.a)
SYNOPSIS
#include <sys/types.h>
#include <sys/socket.h>
int connect(
int s ,
struct sockaddr *name ,
int namelen );
PARAMETERS
s Represents a socket descriptor.
name Contains the name of the socket that s is trying to connect
with.
namelen Specifies the amount of space pointed to by name.
DESCRIPTION
The s parameter is a socket. If it is of type SOCK_DGRAM (see socket(2)),
this call specifies the peer the socket is to be associated with. This is
the address datagrams will be sent to and the only address datagrams will
be received from. If the socket is of type SOCK_STREAM, this call
attempts to connect to another socket. The other socket is specified by
name, which is an address in the communications space of the socket. Each
communications space interprets the name parameter in its own way.
Generally, stream sockets may successfully use the connect() function only
once; datagram sockets may use connect() multiple times to change their
association. Datagram sockets may dissolve the association by connecting
to an invalid address, such as a null address.
EXAMPLES
To connect to a socket in the Internet domain at port 2000 on machine foo:
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
struct sockaddr_in sin;
struct hostent *hp;
2/94 - Intergraph Corporation 1
connect(2) CLIX connect(2)
if (!(hp = gethostbyname("foo"))) {
perror("Gethostbyname failed");
exit();
}
sin.sin_family = hp->h_addrtype;
sin.sin_port = htons(2000);
memcpy((char *)&sin.sin_addr, hp->h_addr, hp->h_length);
if (connect(sd, (struct sockaddr *)&sin, sizeof(sin)) == -1)
perror("Connect failed");
RETURN VALUES
Upon successful completion, a value of 0 is returned. Otherwise, a value
of -1 is returned and errno is set to indicate the error.
ERRORS
The connect() function fails if one or more of the following is true:
[EBADF]
The descriptor is not valid.
[ENOTSOCK]
The descriptor references a file, not a socket.
[EINVAL]
The namelen parameter is not the expected size.
[EFAULT]
The name parameter specifies an area outside the user address
space.
[EADDRNOTAVAIL]
The specified address is not available on this machine.
[EISCONN]
The socket is already connected.
[ECONNREFUSED]
The attempt to connect was forcefully rejected.
[ENETUNREACH]
The network cannot be reached from this host.
[EADDRINUSE]
The address is already in use.
The following errors are specific to connecting names in the UNIX domain.
These errors may not apply in future versions of the UNIX IPC domain.
2 Intergraph Corporation - 2/94
connect(2) CLIX connect(2)
[ENOTDIR]
A component of the path prefix is not a directory.
[ENOENT]
The named socket does not exist.
[EACCES]
Search permission is denied for a component of the path prefix or
write access to the named socket is denied.
RELATED INFORMATION
Functions: accept(2), select(2), socket(2), getsockname(2)
2/94 - Intergraph Corporation 3