5 connect - initiate a connection on a socket
8 #include <network.h>
10 int connect(int sockfd, struct sockaddr *serv_addr, int
14 The parameter sockfd is a socket. If the socket is of
15 type SOCK_DGRAM, this call specifies the peer with which
16 the socket is to be associated; this address is that to
17 which datagrams are to be sent, and the only address from
18 which datagrams are to be received. If the socket is of
19 type SOCK_STREAM, this call attempts to make a connection
20 to another socket. The other socket is specified by
21 serv_addr, which is an address in the communications space
22 of the socket. Each communications space interprets the
23 serv_addr parameter in its own way. Generally, stream
24 sockets may successfully connect only once; datagram sock-
25 ets may use connect multiple times to change their associ-
26 ation. Datagram sockets may dissolve the association by
27 connecting to an address with the sa_family sockaddr mem-
31 If the connection or binding succeeds, zero is returned.
32 On error, -1 is returned, and errno is set appropriately.
35 The following are general socket errors only. There may
36 be other domain-specific error codes.
41 The descriptor is not associated with a socket.
43 EISCONN The socket is already connected.
46 Connection refused at server.
49 Timeout while attempting connection.
52 Network is unreachable.
55 Address is already in use.
58 The socket is non-blocking and the connection can-
59 not be completed immediately. It is possible to
60 select(2) or poll(2) for completion by selecting
61 the socket for writing. After select indicates
62 writability, use getsockopt(2) to read the
63 SO_ERROR option at level SOL_SOCKET to determine
64 whether connect completed successfully (SO_ERROR
65 is zero) or unsuccessfully (SO_ERROR is one of the
66 usual error codes listed above, explaining the
67 reason for the failure).
70 The socket is non-blocking and a previous connec-
71 tion attempt has not yet been completed.
74 The passed address didn't have the correct address
75 family in its sa_family field.
77 EACCES The user tried to connect to a broadcast address
78 without having the socket broadcast flag enabled.