3 memcached_return
memcached_connect(memcached_st
*ptr
)
6 struct sockaddr_in localAddr
, servAddr
;
8 char *server_name
= "localhost";
11 return MEMCACHED_SUCCESS
;
14 if ((h
= gethostbyname(server_name
)) == NULL
)
16 fprintf(stderr
, "unknown host '%s'\n", server_name
);
17 return MEMCACHED_HOST_LOCKUP_FAILURE
;
20 servAddr
.sin_family
= h
->h_addrtype
;
21 memcpy((char *) &servAddr
.sin_addr
.s_addr
, h
->h_addr_list
[0], h
->h_length
);
22 servAddr
.sin_port
= htons(MEMCACHED_DEFAULT_PORT
);
24 /* Create the socket */
25 if ((ptr
->fd
= socket(AF_INET
, SOCK_STREAM
, 0)) < 0)
27 fprintf(stderr
, "cannot open socket");
28 return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE
;
32 /* bind any port number */
33 localAddr
.sin_family
= AF_INET
;
34 localAddr
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
35 localAddr
.sin_port
= htons(0);
37 if (bind(ptr
->fd
, (struct sockaddr
*) &localAddr
, sizeof(localAddr
)) < 0)
39 fprintf(stderr
, "cannot bind port TCP %u\n", MEMCACHED_DEFAULT_PORT
);
40 return(MEMCACHED_CONNECTION_BIND_FAILURE
);
43 /* connect to server */
44 if (connect(ptr
->fd
, (struct sockaddr
*) &servAddr
, sizeof(servAddr
)) < 0)
46 fprintf(stderr
, "cannot connect to host '%s'\n", server_name
);
47 return MEMCACHED_HOST_LOCKUP_FAILURE
;
52 return MEMCACHED_SUCCESS
;