2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 // libs/cutils/socket_network_client.c
27 #include <sys/socket.h>
28 #include <sys/select.h>
29 #include <sys/types.h>
30 #include <netinet/in.h>
35 /* Connect to port on the IP interface. type is
36 * SOCK_STREAM or SOCK_DGRAM.
37 * return is a file descriptor or -1 on error
39 int socket_network_client(const char *host, int port, int type)
41 struct hostent hostbuf, *hp;
42 struct sockaddr_in addr;
44 size_t hstbuflen = 1024;
48 tmphstbuf = malloc(hstbuflen);
49 if (tmphstbuf == NULL) {
53 while ((res = gethostbyname_r(host, &hostbuf, tmphstbuf, hstbuflen, &hp, &herr)) == ERANGE) {
56 void *tmpbuf = realloc(tmphstbuf, hstbuflen);
58 if (tmphstbuf != NULL) {
66 if (res || hp == NULL) {
67 if (tmphstbuf != NULL) {
72 memset(&addr, 0, sizeof(addr));
73 addr.sin_family = hp->h_addrtype;
74 addr.sin_port = htons(port);
75 memcpy(&addr.sin_addr, hp->h_addr, hp->h_length);
77 s = socket(hp->h_addrtype, type, 0);
79 if (tmphstbuf != NULL) {
85 if(connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
88 if (tmphstbuf != NULL) {
93 if (tmphstbuf != NULL) {