Java Code Examples for java.net.SocketAddress.toString()

The following are Jave code examples for showing how to use toString() of the java.net.SocketAddress class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: wechat-mall   File: RemotingHelper.java   View Source Code Vote up 8 votes
public static String parseChannelRemoteAddr(final Channel channel) {
    if (null == channel) {
        return "";
    }
    final SocketAddress remote = channel.remoteAddress();
    final String addr = remote != null ? remote.toString() : "";

    if (addr.length() > 0) {
        int index = addr.lastIndexOf("/");
        if (index >= 0) {
            return addr.substring(index + 1);
        }

        return addr;
    }

    return "";
}
 
Example 2
Project: Backmemed   File: UserListIPBans.java   View Source Code Vote up 6 votes
private String addressToString(SocketAddress address)
{
    String s = address.toString();

    if (s.contains("/"))
    {
        s = s.substring(s.indexOf(47) + 1);
    }

    if (s.contains(":"))
    {
        s = s.substring(0, s.indexOf(58));
    }

    return s;
}
 
Example 3
Project: BaseClient   File: BanList.java   View Source Code Vote up 6 votes
private String addressToString(SocketAddress address)
{
    String s = address.toString();

    if (s.contains("/"))
    {
        s = s.substring(s.indexOf(47) + 1);
    }

    if (s.contains(":"))
    {
        s = s.substring(0, s.indexOf(58));
    }

    return s;
}
 
Example 4
Project: JavaQuarkBBS   File: NettyUtil.java   View Source Code Vote up 6 votes
/**
 * 获取Channel的远程IP地址
 * @param channel
 * @return
 */
public static String parseChannelRemoteAddr(final Channel channel) {
    if (null == channel) {
        return "";
    }
    SocketAddress remote = channel.remoteAddress();
    final String addr = remote != null ? remote.toString() : "";

    if (addr.length() > 0) {
        int index = addr.lastIndexOf("/");
        if (index >= 0) {
            return addr.substring(index + 1);
        }

        return addr;
    }

    return "";
}
 
Example 5
Project: BaseClient   File: BanList.java   View Source Code Vote up 6 votes
private String addressToString(SocketAddress address)
{
    String s = address.toString();

    if (s.contains("/"))
    {
        s = s.substring(s.indexOf(47) + 1);
    }

    if (s.contains(":"))
    {
        s = s.substring(0, s.indexOf(58));
    }

    return s;
}
 
Example 6
Project: CustomWorldGen   File: UserListIPBans.java   View Source Code Vote up 6 votes
private String addressToString(SocketAddress address)
{
    String s = address.toString();

    if (s.contains("/"))
    {
        s = s.substring(s.indexOf(47) + 1);
    }

    if (s.contains(":"))
    {
        s = s.substring(0, s.indexOf(58));
    }

    return s;
}
 
Example 7
Project: DistributedID   File: NettyUtil.java   View Source Code Vote up 6 votes
/**
 * 获取Channel的远程IP地址
 * @param channel
 * @return
 */
public static String parseRemoteAddr(final Channel channel) {
    if (null == channel) {
        return "";
    }
    SocketAddress remote = channel.remoteAddress();
    final String addr = remote != null ? remote.toString() : "";

    if (addr.length() > 0) {
        int index = addr.lastIndexOf("/");
        if (index >= 0) {
            return addr.substring(index + 1);
        }

        return addr;
    }

    return "";
}
 
Example 8
Project: reading-and-annotate-rocketmq-3.4.6   File: RemotingHelper.java   View Source Code Vote up 6 votes
public static String parseChannelRemoteAddr(final Channel channel) {
    if (null == channel) {
        return "";
    }
    final SocketAddress remote = channel.remoteAddress();
    final String addr = remote != null ? remote.toString() : "";

    if (addr.length() > 0) {
        int index = addr.lastIndexOf("/");
        if (index >= 0) {
            return addr.substring(index + 1);
        }

        return addr;
    }

    return "";
}
 
Example 9
Project: ConfigCenter   File: RemotingHelper.java   View Source Code Vote up 6 votes
public static String parseChannelRemoteAddr(final Channel channel) {
    if (null == channel) {
        return "";
    }
    SocketAddress remote = channel.remoteAddress();
    final String addr = remote != null ? remote.toString() : "";

    if (addr.length() > 0) {
        int index = addr.lastIndexOf("/");
        if (index >= 0) {
            return addr.substring(index + 1);
        }

        return addr;
    }

    return "";
}
 
Example 10
Project: HappyChat   File: NettyUtil.java   View Source Code Vote up 6 votes
/**
 * 获取Channel的远程IP地址
 * @param channel
 * @return
 */
public static String parseChannelRemoteAddr(final Channel channel) {
    if (null == channel) {
        return "";
    }
    SocketAddress remote = channel.remoteAddress();
    final String addr = remote != null ? remote.toString() : "";

    if (addr.length() > 0) {
        int index = addr.lastIndexOf("/");
        if (index >= 0) {
            return addr.substring(index + 1);
        }

        return addr;
    }

    return "";
}
 
Example 11
Project: Lantern-sdk   File: LanternSocketImpl.java   View Source Code Vote up 6 votes
protected void connect(SocketAddress remoteAddr, int timeout) throws IOException {
    if(remoteAddr instanceof InetSocketAddress) {
        InetSocketAddress e = (InetSocketAddress)remoteAddr;
        this.name = e.getHostName();
        this.setProtocolFromPort(e.getPort());
    } else {
        this.name = remoteAddr.toString();
    }
    Logger.d("Socket connect", this.name);

    try {
        this.delegator.invoke(new Object[]{remoteAddr, Integer.valueOf(timeout)});
    } catch (Exception var6) {
        Exception e1 = var6;
        if(var6 instanceof IOException) {
            throw (IOException)var6;
        }
        try {
            throw e1.getCause();
        } catch (Throwable var5) {
            var5.printStackTrace();
        }
    }

    this.startTime = System.currentTimeMillis();
}
 
Example 12
Project: DistributedID-SDK   File: NettyUtil.java   View Source Code Vote up 6 votes
/**
 * 获取Channel的远程IP地址
 * @param channel
 * @return
 */
public static String parseRemoteAddr(final Channel channel) {
    if (null == channel) {
        return "";
    }
    SocketAddress remote = channel.remoteAddress();
    final String addr = remote != null ? remote.toString() : "";

    if (addr.length() > 0) {
        int index = addr.lastIndexOf("/");
        if (index >= 0) {
            return addr.substring(index + 1);
        }

        return addr;
    }

    return "";
}
 
Example 13
Project: wechat-mall   File: RemotingHelper.java   View Source Code Vote up 5 votes
public static String parseSocketAddressAddr(SocketAddress socketAddress) {
    if (socketAddress != null) {
        final String addr = socketAddress.toString();

        if (addr.length() > 0) {
            return addr.substring(1);
        }
    }
    return "";
}
 
Example 14
Project: cas-server-4.2.1   File: MemcachedMonitor.java   View Source Code Vote up 5 votes
/**
 * Get cache statistics for all memcached hosts known to {@link MemcachedClientIF}.
 *
 * @return Statistics for all available hosts.
 */
@Override
protected CacheStatistics[] getStatistics() {


    final Map<SocketAddress, Map<String, String>> allStats = memcachedClient.getStats();
    final List<CacheStatistics> statsList = new ArrayList<>();
    for (final Map.Entry<SocketAddress, Map<String, String>> entry : allStats.entrySet()) {
        final SocketAddress key = entry.getKey();
        final Map<String, String> statsMap = entry.getValue();

        if (!statsMap.isEmpty()) {
            final long size = Long.parseLong(statsMap.get("bytes"));
            final long capacity = Long.parseLong(statsMap.get("limit_maxbytes"));
            final long evictions = Long.parseLong(statsMap.get("evictions"));

            final String name;
            if (key instanceof InetSocketAddress) {
                name = ((InetSocketAddress) key).getHostName();
            } else {
                name = key.toString();
            }
            statsList.add(new SimpleCacheStatistics(size, capacity, evictions, name));
        }
    }
    return statsList.toArray(new CacheStatistics[statsList.size()]);
}
 
Example 15
Project: rocketmq-rocketmq-all-4.1.0-incubating   File: RemotingHelper.java   View Source Code Vote up 5 votes
public static String parseSocketAddressAddr(SocketAddress socketAddress) {
    if (socketAddress != null) {
        final String addr = socketAddress.toString();

        if (addr.length() > 0) {
            return addr.substring(1);
        }
    }
    return "";
}
 
Example 16
Project: elephant   File: RemotingHelper.java   View Source Code Vote up 5 votes
public static String parseSocketAddressAddr(SocketAddress socketAddress) {
    if (socketAddress != null) {
        final String addr = socketAddress.toString();

        if (addr.length() > 0) {
            return addr.substring(1);
        }
    }
    return "";
}
 
Example 17
Project: rmq4note   File: RemotingHelper.java   View Source Code Vote up 5 votes
public static String parseSocketAddressAddr(SocketAddress socketAddress) {
    if (socketAddress != null) {
        final String addr = socketAddress.toString();

        if (addr.length() > 0) {
            return addr.substring(1);
        }
    }
    return "";
}
 
Example 18
Project: ConfigCenter   File: NettyRemotingClient.java   View Source Code Vote up 5 votes
@Override
public void connect(ChannelHandlerContext ctx, SocketAddress remoteAddress, SocketAddress localAddress, ChannelPromise promise)
        throws Exception {
    final String local = localAddress == null ? "UNKNOW" : localAddress.toString();
    final String remote = remoteAddress == null ? "UNKNOW" : remoteAddress.toString();
    log.info("NETTY CLIENT PIPELINE: CONNECT  {} => {}", local, remote);
    super.connect(ctx, remoteAddress, localAddress, promise);

    if (NettyRemotingClient.this.channelEventListener != null) {
        NettyRemotingClient.this.putNettyEvent(new NettyEvent(NettyEventType.CONNECT, remoteAddress.toString(), ctx.channel()));
    }
}
 
Example 19
Project: simulacron   File: EndpointIntegrationTest.java   View Source Code Vote up 4 votes
@Test
public void testDeleteConnections() throws Exception {
  Collection<BoundDataCenter> datacenters = server.getCluster().getDataCenters();
  BoundDataCenter dc = datacenters.iterator().next();
  Iterator<BoundNode> nodeIterator = dc.getNodes().iterator();
  BoundNode node = nodeIterator.next();

  ArrayList<Scope> list = new ArrayList<>();
  list.add(new Scope(server.getCluster().getId(), dc.getId(), node.getId()));
  list.add(new Scope(server.getCluster().getId(), dc.getId(), null));
  list.add(new Scope(server.getCluster().getId(), null, null));

  for (Scope scope : list) {
    try (com.datastax.driver.core.Cluster driverCluster =
        defaultBuilder()
            .addContactPointsWithPorts((InetSocketAddress) node.getAddress())
            .build()) {
      driverCluster.init();
      HttpTestResponse responseDelete =
          server.delete("/connections/" + scope.toString() + "?type=disconnect");

      ClusterConnectionReport responseReport =
          om.readValue(responseDelete.body, ClusterConnectionReport.class);
      Collection<NodeConnectionReport> nodes =
          getNodeConnectionReports(responseReport, dc.getId());

      assertThat(responseDelete.response.statusCode()).isEqualTo(200);

      HttpTestResponse responseNewConnections = server.get("/connections/" + scope.toString());
      assertThat(responseNewConnections.body).isNotEqualTo(responseDelete.body);
      for (NodeConnectionReport nodeReport : nodes) {
        for (SocketAddress sA : nodeReport.getConnections()) {
          String sAString = sA.toString();
          assertThat(responseDelete.body).contains(sAString.substring(1, sAString.length()));
          assertThat(responseNewConnections.body)
              .doesNotContain(sAString.substring(1, sAString.length()));
        }
      }
    }
  }
}
 
Example 20
Project: monarch   File: ServerHandShakeProcessor.java   View Source Code Vote up 4 votes
private static Version readClientVersion(ServerConnection connection)
    throws IOException, VersionException {

  Socket socket = connection.getSocket();
  int timeout = connection.getHandShakeTimeout();

  int soTimeout = -1;
  try {
    soTimeout = socket.getSoTimeout();
    socket.setSoTimeout(timeout);
    InputStream is = socket.getInputStream();
    short clientVersionOrdinal = Version.readOrdinalFromInputStream(is);
    if (clientVersionOrdinal == -1) {
      throw new EOFException(
          LocalizedStrings.ServerHandShakeProcessor_HANDSHAKEREADER_EOF_REACHED_BEFORE_CLIENT_VERSION_COULD_BE_READ
              .toLocalizedString());
    }
    Version clientVersion = null;
    try {
      clientVersion = Version.fromOrdinal(clientVersionOrdinal, true);
    } catch (UnsupportedVersionException uve) {
      // Allows higher version of wan site to connect to server
      if (connection.getCommunicationMode() == Acceptor.GATEWAY_TO_GATEWAY
          && !(clientVersionOrdinal == Version.NOT_SUPPORTED_ORDINAL)) {
        return Acceptor.VERSION;
      } else {
        SocketAddress sa = socket.getRemoteSocketAddress();
        String sInfo = "";
        if (sa != null) {
          sInfo = " Client: " + sa.toString() + ".";
        }
        throw new UnsupportedVersionException(uve.getMessage() + sInfo);
      }
    }

    if (!clientVersion.compatibleWith(Acceptor.VERSION)) {
      throw new IncompatibleVersionException(clientVersion, Acceptor.VERSION);// we can throw this
                                                                              // to restrict
    } // Backward Compatibilty Support to limited no of versions
    return clientVersion;
  } finally {
    if (soTimeout != -1) {
      try {
        socket.setSoTimeout(soTimeout);
      } catch (IOException ignore) {
      }
    }
  }
}