![]() Chapter Contents  | 
![]() Previous  | 
![]() Next  | 
| connect | 
| Portability: | UNIX compatible | 
| SYNOPSIS | |
| DESCRIPTION | |
| RETURN VALUE | |
| PORTABILITY | |
| EXAMPLE | |
| RELATED FUNCTIONS | 
| SYNOPSIS | 
#include <sys/types.h> #include <sys/socket.h> int connect(int s, const void *addr, int addrlen);
| DESCRIPTION | 
connect
 attempts to associate socket 
s
 with a peer process at address 
addr
. 
addr
 is a
pointer to a buffer containing the address of the peer socket. 
addrlen
 is the length, in bytes, of the buffer
pointed to by 
addr
. 
addrlen
 should be greater than
or equal to the number of bytes in the 
sockaddr
 or 
sockaddr_in
 structure.
For connection-oriented protocols on blocking sockets,
when the establishment of a connection is actually attempted, the call blocks
I/O  until the connection attempt succeeds or fails.  On non-blocking sockets,
the call returns immediately, with 
errno
 set to 
EINPROGRESS
 if the connection could not complete immediately.  The caller can
then discover whether or not the connection is complete by issuing the 
select
 call to determine if the
socket is ready for writing.
For sockets that use connectionless protocols, 
connect
 enables the socket to
register a destination address once, instead of having to specify the same
destination address for every read or write operation.  By associating a packet
with a specific socket, 
connect
 provides more information with which to trace the source of the problem
if a transmission fails. In this case, 
connect
 can be called many times.
| RETURN VALUE | 
If 
connect
 is successful, it returns a 
0
; otherwise, it returns a 
-1
 and sets 
errno
to indicate the type of error.
If 
errno
 is set to 
ECONNREFUSED
, reuse of the failed socket descriptor is TCP/IP implementation-specific
and unpredictable. However, it is always safe to close and obtain another
socket descriptor for subsequent calls to 
connect
.
| PORTABILITY | 
connect
 is portable to other environments, including most UNIX systems,
that implement BSD sockets.
| EXAMPLE | 
In this example, 
connect
 connects socket 
s
 to a specific host and port.  
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
f()
{
   struct sockaddr_in sa;
   struct hostent *host;
   struct servent *serv;
   int s;
   .
   .
   .
      /* Specify destination socket address (IP address and */
      /* port number).                                      */
   memset(&sa,'\0',sizeof(sa));
   sa.sin_family = AF_INET;
   memcpy(&sa.sin_addr,host->h_addr,sizeof(sa.sin_addr));
   sa.sin_port = serv->s_port;
      /* Connect to the host and port.                      */
   if (connect(s, &sa, sizeof(sa)) == -1) {
      perror("connect() failed");
      return -1;
   }
   .
   .
   .
}
| RELATED FUNCTIONS | 
accept
, 
select
, 
socket
, 
getpeername
![]() Chapter Contents  | 
![]() Previous  | 
![]() Next  | 
![]() Top of Page  | 
Copyright © 2001 by SAS Institute Inc., Cary, NC, USA. All rights reserved.