static UnixSocketAddress getsockname(int sockfd) { UnixSocketAddress local = new UnixSocketAddress(); SockAddrUnix addr = local.getStruct(); IntByReference len = new IntByReference(addr.getMaximumLength()); if (Native.libc().getsockname(sockfd, addr, len) < 0) { throw new Error(Native.getLastErrorString()); } addr.updatePath(len.getValue()); return local; }
static UnixSocketAddress getpeername(int sockfd) { UnixSocketAddress remote = new UnixSocketAddress(); SockAddrUnix addr = remote.getStruct(); IntByReference len = new IntByReference(addr.getMaximumLength()); if (Native.libc().getpeername(sockfd, addr, len) < 0) { throw new Error(Native.getLastErrorString()); } addr.updatePath(len.getValue()); return remote; }
public UnixSocketChannel accept() throws IOException { UnixSocketAddress remote = new UnixSocketAddress(); SockAddrUnix addr = remote.getStruct(); int maxLength = addr.getMaximumLength(); IntByReference len = new IntByReference(maxLength); int clientfd = Native.accept(getFD(), addr, len); if (clientfd < 0) { if (isBlocking()) { throw new IOException("accept failed: " + Native.getLastErrorString()); } return null; } // Handle unnamed sockets and sockets in Linux' abstract namespace addr.updatePath(len.getValue()); // Always force the socket back to blocking mode Native.setBlocking(clientfd, true); return new UnixSocketChannel(clientfd); }