Search in sources :

Example 96 with SocketAddress

use of java.net.SocketAddress in project hazelcast by hazelcast.

the class InitConnectionTask method bindSocket.

private void bindSocket(SocketChannel socketChannel) throws IOException {
    InetAddress inetAddress = getInetAddress();
    Socket socket = socketChannel.socket();
    if (connectionManager.useAnyOutboundPort()) {
        SocketAddress socketAddress = new InetSocketAddress(inetAddress, 0);
        socket.bind(socketAddress);
    } else {
        IOException ex = null;
        int retryCount = connectionManager.getOutboundPortCount() * 2;
        for (int i = 0; i < retryCount; i++) {
            int port = connectionManager.acquireOutboundPort();
            SocketAddress socketAddress = new InetSocketAddress(inetAddress, port);
            try {
                socket.bind(socketAddress);
                return;
            } catch (IOException e) {
                ex = e;
                logger.finest("Could not bind port[ " + port + "]: " + e.getMessage());
            }
        }
        throw ex;
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) IOException(java.io.IOException) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) InetAddress(java.net.InetAddress) Socket(java.net.Socket)

Example 97 with SocketAddress

use of java.net.SocketAddress in project gitblit by gitblit.

the class SshServerSessionFactory method createSession.

@Override
protected AbstractSession createSession(final IoSession io) throws Exception {
    log.info("creating ssh session from {}", io.getRemoteAddress());
    if (io instanceof MinaSession) {
        if (((MinaSession) io).getSession().getConfig() instanceof SocketSessionConfig) {
            ((SocketSessionConfig) ((MinaSession) io).getSession().getConfig()).setKeepAlive(true);
        }
    }
    final SshServerSession session = (SshServerSession) super.createSession(io);
    SocketAddress peer = io.getRemoteAddress();
    SshDaemonClient client = new SshDaemonClient(peer);
    session.setAttribute(SshDaemonClient.KEY, client);
    // TODO(davido): Log a session close without authentication as a
    // failure.
    session.addCloseSessionListener(new SshFutureListener<CloseFuture>() {

        @Override
        public void operationComplete(CloseFuture future) {
            log.info("closed ssh session from {}", io.getRemoteAddress());
        }
    });
    return session;
}
Also used : CloseFuture(org.apache.sshd.common.future.CloseFuture) MinaSession(org.apache.sshd.common.io.mina.MinaSession) SocketAddress(java.net.SocketAddress) SocketSessionConfig(org.apache.mina.transport.socket.SocketSessionConfig)

Example 98 with SocketAddress

use of java.net.SocketAddress in project j2objc by google.

the class DatagramChannelImpl method receiveImpl.

private SocketAddress receiveImpl(ByteBuffer target, boolean loop) throws IOException {
    SocketAddress retAddr = null;
    DatagramPacket receivePacket;
    int oldposition = target.position();
    int received;
    // TODO: disallow mapped buffers and lose this conditional?
    if (target.hasArray()) {
        receivePacket = new DatagramPacket(target.array(), target.position() + target.arrayOffset(), target.remaining());
    } else {
        receivePacket = new DatagramPacket(new byte[target.remaining()], target.remaining());
    }
    do {
        received = NetworkBridge.recvfrom(false, fd, receivePacket.getData(), receivePacket.getOffset(), receivePacket.getLength(), 0, receivePacket, isConnected());
        if (receivePacket.getAddress() != null) {
            if (received > 0) {
                if (target.hasArray()) {
                    target.position(oldposition + received);
                } else {
                    // copy the data of received packet
                    target.put(receivePacket.getData(), 0, received);
                }
            }
            retAddr = receivePacket.getSocketAddress();
            break;
        }
    } while (loop);
    return retAddr;
}
Also used : DatagramPacket(java.net.DatagramPacket) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress)

Example 99 with SocketAddress

use of java.net.SocketAddress in project j2objc by google.

the class DatagramChannelImpl method onBind.

/**
     * Initialise the isBound, localAddress and localPort state from the file descriptor. Used when
     * some or all of the bound state has been left to the OS to decide, or when the Socket handled
     * bind() or connect().
     *
     * @param updateSocketState
     *        if the associated socket (if present) needs to be updated
     * @hide used to sync state, non-private to avoid synthetic method
     */
void onBind(boolean updateSocketState) {
    SocketAddress sa;
    try {
        sa = NetworkOs.getsockname(fd);
    } catch (ErrnoException errnoException) {
        throw new AssertionError(errnoException);
    }
    isBound = true;
    InetSocketAddress localSocketAddress = (InetSocketAddress) sa;
    localAddress = localSocketAddress.getAddress();
    localPort = localSocketAddress.getPort();
    if (updateSocketState && socket != null) {
        socket.onBind(localAddress, localPort);
    }
}
Also used : ErrnoException(libcore.io.ErrnoException) InetSocketAddress(java.net.InetSocketAddress) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress)

Example 100 with SocketAddress

use of java.net.SocketAddress in project grpc-java by grpc.

the class PickFirstLoadBalancerTest method setUp.

@Before
public void setUp() {
    MockitoAnnotations.initMocks(this);
    for (int i = 0; i < 3; i++) {
        SocketAddress addr = new FakeSocketAddress("server" + i);
        servers.add(ResolvedServerInfoGroup.builder().add(new ResolvedServerInfo(addr)).build());
        socketAddresses.add(addr);
    }
    when(mockSubchannel.getAddresses()).thenReturn(new EquivalentAddressGroup(socketAddresses));
    when(mockHelper.createSubchannel(any(EquivalentAddressGroup.class), any(Attributes.class))).thenReturn(mockSubchannel);
    loadBalancer = (PickFirstBalancer) PickFirstBalancerFactory.getInstance().newLoadBalancer(mockHelper);
}
Also used : SocketAddress(java.net.SocketAddress) Before(org.junit.Before)

Aggregations

SocketAddress (java.net.SocketAddress)347 InetSocketAddress (java.net.InetSocketAddress)273 IOException (java.io.IOException)74 Test (org.junit.Test)72 Socket (java.net.Socket)46 InetAddress (java.net.InetAddress)34 Channel (org.jboss.netty.channel.Channel)33 SocketException (java.net.SocketException)27 UnknownHostException (java.net.UnknownHostException)24 DefaultHttpResponse (org.jboss.netty.handler.codec.http.DefaultHttpResponse)23 HttpResponse (org.jboss.netty.handler.codec.http.HttpResponse)23 ChannelPipeline (org.jboss.netty.channel.ChannelPipeline)20 ConditionCheck (com.linkedin.databus2.test.ConditionCheck)19 SimpleObjectCaptureHandler (com.linkedin.databus2.test.container.SimpleObjectCaptureHandler)19 HashMap (java.util.HashMap)18 Proxy (java.net.Proxy)17 ByteBuffer (java.nio.ByteBuffer)16 Logger (org.apache.log4j.Logger)16 Test (org.testng.annotations.Test)16 DatagramPacket (java.net.DatagramPacket)15