Search in sources :

Example 26 with SocketAddress

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

the class ManagedSocketFactoryTest method createMulticastSocket.

@Test
public void createMulticastSocket() throws IOException {
    MulticastSocket socket1 = mock(MulticastSocket.class);
    MulticastSocket socket2 = mock(MulticastSocket.class);
    MulticastSocket socket3 = mock(MulticastSocket.class);
    MulticastSocket socket4 = mock(MulticastSocket.class);
    SocketAddress address = new InetSocketAddress(InetAddress.getLocalHost(), 1);
    when(this.manager.createMulticastSocket("test", new InetSocketAddress(0))).thenReturn(socket1);
    when(this.manager.createMulticastSocket("test", new InetSocketAddress(1))).thenReturn(socket2);
    when(this.manager.createMulticastSocket("test", address)).thenReturn(socket3);
    when(this.manager.createMulticastSocket("test")).thenReturn(socket4);
    MulticastSocket result1 = this.subject.createMulticastSocket("test");
    MulticastSocket result2 = this.subject.createMulticastSocket("test", 1);
    MulticastSocket result3 = this.subject.createMulticastSocket("test", address);
    MulticastSocket result4 = this.subject.createMulticastSocket("test", null);
    assertSame(socket1, result1);
    assertSame(socket2, result2);
    assertSame(socket3, result3);
    assertSame(socket4, result4);
}
Also used : MulticastSocket(java.net.MulticastSocket) InetSocketAddress(java.net.InetSocketAddress) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) Test(org.junit.Test)

Example 27 with SocketAddress

use of java.net.SocketAddress in project heron by twitter.

the class HeronServer method handleError.

// Clean the stuff when meeting some errors
public void handleError(SelectableChannel channel) {
    SocketAddress channelAddress = ((SocketChannel) channel).socket().getRemoteSocketAddress();
    LOG.info("Handling error from channel: " + channelAddress);
    SocketChannelHelper helper = activeConnections.get(channel);
    if (helper == null) {
        LOG.severe("Inactive channel had error?");
        return;
    }
    helper.clear();
    LOG.info("Removing all interest on channel: " + channelAddress);
    nioLooper.removeAllInterest(channel);
    try {
        channel.close();
    } catch (IOException e) {
        LOG.severe("Error closing connection in handleError");
    }
    activeConnections.remove(channel);
    onClose((SocketChannel) channel);
}
Also used : IOException(java.io.IOException) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress)

Example 28 with SocketAddress

use of java.net.SocketAddress in project jackrabbit-oak by apache.

the class Utils method setProxyIfNeeded.

public static void setProxyIfNeeded(final Properties properties) {
    String proxyHost = properties.getProperty(AzureConstants.PROXY_HOST);
    String proxyPort = properties.getProperty(AzureConstants.PROXY_PORT);
    if (!Strings.isNullOrEmpty(proxyHost) && Strings.isNullOrEmpty(proxyPort)) {
        int port = Integer.parseInt(proxyPort);
        SocketAddress proxyAddr = new InetSocketAddress(proxyHost, port);
        Proxy proxy = new Proxy(Proxy.Type.HTTP, proxyAddr);
        OperationContext.setDefaultProxy(proxy);
    }
}
Also used : Proxy(java.net.Proxy) InetSocketAddress(java.net.InetSocketAddress) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress)

Example 29 with SocketAddress

use of java.net.SocketAddress in project geode by apache.

the class JmxManagerFinder method askLocatorForJmxManager.

/**
   * Ask a locator to find a jmx manager. The locator will start one if one is not already running.
   * 
   * @param addr the host address the locator is listening on
   * @param port the port the locator is listening on
   * @param timeout the number of milliseconds to wait for a response; 15000 is a reasonable default
   * @return describes the location of the jmx manager. The port will be zero if no jmx manager was
   *         found.
   * @throws IOException if a problem occurs trying to connect to the locator or communicate with
   *         it.
   */
public static JmxManagerInfo askLocatorForJmxManager(InetAddress addr, int port, int timeout, boolean usessl) throws IOException {
    SocketAddress sockaddr = new InetSocketAddress(addr, port);
    Socket sock = ConnectionUtil.getSocketFactory(usessl).createSocket();
    try {
        sock.connect(sockaddr, timeout);
        sock.setSoTimeout(timeout);
        DataOutputStream out = new DataOutputStream(sock.getOutputStream());
        out.writeInt(GOSSIPVERSION);
        out.writeByte(DS_FIXED_ID_SHORT);
        out.writeShort(JMX_MANAGER_LOCATOR_REQUEST);
        out.flush();
        DataInputStream in = new DataInputStream(sock.getInputStream());
        byte header = in.readByte();
        if (header != DS_FIXED_ID_SHORT) {
            throw new IllegalStateException("Expected " + DS_FIXED_ID_SHORT + " but found " + header);
        }
        int msgType = in.readShort();
        if (msgType != JMX_MANAGER_LOCATOR_RESPONSE) {
            throw new IllegalStateException("Expected " + JMX_MANAGER_LOCATOR_RESPONSE + " but found " + msgType);
        }
        byte hostHeader = in.readByte();
        String host;
        if (hostHeader == NULL_STRING) {
            host = "";
        } else if (hostHeader == STRING_BYTES) {
            int len = in.readUnsignedShort();
            byte[] buf = new byte[len];
            in.readFully(buf, 0, len);
            @SuppressWarnings("deprecation") String str = new String(buf, 0);
            host = str;
        } else {
            throw new IllegalStateException("Expected " + STRING_BYTES + " or " + NULL_STRING + " but found " + hostHeader);
        }
        int jmport = in.readInt();
        boolean ssl = in.readBoolean();
        if (host.equals("")) {
            jmport = 0;
        }
        return new JmxManagerInfo(host, jmport, ssl);
    } finally {
        try {
            sock.close();
        } catch (Exception e) {
        }
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) DataOutputStream(java.io.DataOutputStream) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) DataInputStream(java.io.DataInputStream) Socket(java.net.Socket) IOException(java.io.IOException)

Example 30 with SocketAddress

use of java.net.SocketAddress in project jackrabbit-oak by apache.

the class ClientFilterHandler method channelActive.

@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
    SocketAddress address = ctx.channel().remoteAddress();
    if (filter.isAllowed(address)) {
        log.debug("Client {} is allowed", address);
        ctx.fireChannelActive();
    } else {
        log.debug("Client {} is rejected", address);
        ctx.close();
    }
}
Also used : SocketAddress(java.net.SocketAddress)

Aggregations

SocketAddress (java.net.SocketAddress)717 InetSocketAddress (java.net.InetSocketAddress)542 Test (org.junit.Test)169 IOException (java.io.IOException)154 Socket (java.net.Socket)105 InetAddress (java.net.InetAddress)56 EquivalentAddressGroup (io.grpc.EquivalentAddressGroup)42 Proxy (java.net.Proxy)39 ArrayList (java.util.ArrayList)37 SocketChannel (java.nio.channels.SocketChannel)36 SocketException (java.net.SocketException)34 ServerSocket (java.net.ServerSocket)33 HashMap (java.util.HashMap)33 Channel (org.jboss.netty.channel.Channel)33 UnknownHostException (java.net.UnknownHostException)32 ByteBuffer (java.nio.ByteBuffer)32 Map (java.util.Map)31 Set (java.util.Set)31 Channel (io.netty.channel.Channel)30 HashSet (java.util.HashSet)30