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_inaddr_any_server.c
27 #include <sys/socket.h>
28 #include <sys/select.h>
29 #include <sys/types.h>
30 #include <netinet/in.h>
33 #define LISTEN_BACKLOG 4
35 /* open listen() port on any interface */
36 int socket_inaddr_any_server(int port, int type)
38 struct sockaddr_in addr;
41 memset(&addr, 0, sizeof(addr));
42 addr.sin_family = AF_INET;
43 addr.sin_port = htons(port);
44 addr.sin_addr.s_addr = htonl(INADDR_ANY);
46 s = socket(AF_INET, type, 0);
50 if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &n, sizeof(n)) < 0) {
55 if(bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
60 if (type == SOCK_STREAM) {
63 ret = listen(s, LISTEN_BACKLOG);