2 * Copyright (C) 2007 The Android Open Source Project
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.
19 import java.io.IOException;
20 import java.io.FileDescriptor;
23 * Non-standard class for creating an inbound UNIX-domain socket
24 * in the Linux abstract namespace.
26 public class LocalServerSocket {
27 private final LocalSocketImpl impl;
28 private final LocalSocketAddress localAddress;
30 /** 50 seems a bit much, but it's what was here */
31 private static final int LISTEN_BACKLOG = 50;
34 * Creates a new server socket listening at specified name.
35 * On the Android platform, the name is created in the Linux
36 * abstract namespace (instead of on the filesystem).
38 * @param name address for socket
41 public LocalServerSocket(String name) throws IOException
43 impl = new LocalSocketImpl();
45 impl.create(LocalSocket.SOCKET_STREAM);
47 localAddress = new LocalSocketAddress(name);
48 impl.bind(localAddress);
50 impl.listen(LISTEN_BACKLOG);
54 * Create a LocalServerSocket from a file descriptor that's already
55 * been created and bound. listen() will be called immediately on it.
56 * Used for cases where file descriptors are passed in via environment
59 * @param fd bound file descriptor
62 public LocalServerSocket(FileDescriptor fd) throws IOException
64 impl = new LocalSocketImpl(fd);
65 impl.listen(LISTEN_BACKLOG);
66 localAddress = impl.getSockAddress();
70 * Obtains the socket's local address
72 * @return local address
74 public LocalSocketAddress getLocalSocketAddress()
80 * Accepts a new connection to the socket. Blocks until a new
83 * @return a socket representing the new connection.
86 public LocalSocket accept() throws IOException
88 LocalSocketImpl acceptedImpl = new LocalSocketImpl();
90 impl.accept (acceptedImpl);
92 return new LocalSocket(acceptedImpl, LocalSocket.SOCKET_UNKNOWN);
96 * Returns file descriptor or null if not yet open/already closed
100 public FileDescriptor getFileDescriptor() {
101 return impl.getFileDescriptor();
105 * Closes server socket.
107 * @throws IOException
109 public void close() throws IOException