3 memcached_return
memcached_connect(memcached_st
*ptr
)
6 struct sockaddr_in localAddr
, servAddr
;
9 LIBMEMCACHED_MEMCACHED_CONNECT_START();
11 if (ptr
->connected
== ptr
->number_of_hosts
)
12 return MEMCACHED_SUCCESS
;
15 return MEMCACHED_NO_SERVERS
;
17 for (x
= 0; x
< ptr
->number_of_hosts
; x
++)
19 if (ptr
->hosts
[x
].fd
== -1)
21 if ((h
= gethostbyname(ptr
->hosts
[x
].hostname
)) == NULL
)
22 return MEMCACHED_HOST_LOCKUP_FAILURE
;
24 servAddr
.sin_family
= h
->h_addrtype
;
25 memcpy((char *) &servAddr
.sin_addr
.s_addr
, h
->h_addr_list
[0], h
->h_length
);
26 servAddr
.sin_port
= htons(ptr
->hosts
[x
].port
);
28 /* Create the socket */
29 if ((ptr
->hosts
[0].fd
= socket(AF_INET
, SOCK_STREAM
, 0)) < 0)
30 return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE
;
33 /* bind any port number */
34 localAddr
.sin_family
= AF_INET
;
35 localAddr
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
36 localAddr
.sin_port
= htons(0);
38 if (bind(ptr
->hosts
[0].fd
, (struct sockaddr
*) &localAddr
, sizeof(localAddr
)) < 0)
39 return(MEMCACHED_CONNECTION_BIND_FAILURE
);
41 /* connect to server */
42 if (connect(ptr
->hosts
[0].fd
, (struct sockaddr
*) &servAddr
, sizeof(servAddr
)) < 0)
43 return MEMCACHED_HOST_LOCKUP_FAILURE
;
48 LIBMEMCACHED_MEMCACHED_CONNECT_END();
50 return MEMCACHED_SUCCESS
;