java.net.Socket.getPort()方法的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(7.9k)|赞(0)|评价(0)|浏览(377)

本文整理了Java中java.net.Socket.getPort()方法的一些代码示例,展示了Socket.getPort()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Socket.getPort()方法的具体详情如下:
包路径:java.net.Socket
类名称:Socket
方法名:getPort

Socket.getPort介绍

[英]Returns the port number of the target host this socket is connected to, or 0 if this socket is not yet connected.
[中]返回此套接字连接到的目标主机的端口号,如果此套接字尚未连接,则返回0。

代码示例

代码示例来源:origin: square/okhttp

private void processHandshakeFailure(Socket raw) throws Exception {
 SSLContext context = SSLContext.getInstance("TLS");
 context.init(null, new TrustManager[] {UNTRUSTED_TRUST_MANAGER}, new SecureRandom());
 SSLSocketFactory sslSocketFactory = context.getSocketFactory();
 SSLSocket socket = (SSLSocket) sslSocketFactory.createSocket(
   raw, raw.getInetAddress().getHostAddress(), raw.getPort(), true);
 try {
  socket.startHandshake(); // we're testing a handshake failure
  throw new AssertionError();
 } catch (IOException expected) {
 }
 socket.close();
}

代码示例来源:origin: aws/aws-sdk-java

/**
 * Returns the endpoint in the format of "address:port"
 */
private String endpoint() {
  return sock.getInetAddress() + ":" + sock.getPort();
}

代码示例来源:origin: commons-net/commons-net

String host = (_hostname_ != null) ? _hostname_ : getRemoteAddress().getHostAddress();
int port = _socket_.getPort();
SSLSocket socket =
  (SSLSocket) ssf.createSocket(_socket_, host, port, false);

代码示例来源:origin: wildfly/wildfly

protected String getSockAddress() {
  StringBuilder sb=new StringBuilder();
  if(sock != null) {
    sb.append(sock.getLocalAddress().getHostAddress()).append(':').append(sock.getLocalPort());
    sb.append(" - ").append(sock.getInetAddress().getHostAddress()).append(':').append(sock.getPort());
  }
  return sb.toString();
}

代码示例来源:origin: apache/activemq

/**
   * @return pretty print of 'this'
   */
  @Override
  public String toString() {
    return "ssl://" + socket.getInetAddress() + ":" + socket.getPort();
  }
}

代码示例来源:origin: alibaba/cobar

public FrontendConnection(SocketChannel channel) {
  super(channel);
  Socket socket = channel.socket();
  this.host = socket.getInetAddress().getHostAddress();
  this.port = socket.getPort();
  this.localPort = socket.getLocalPort();
  this.handler = new FrontendAuthenticator(this);
}

代码示例来源:origin: robovm/robovm

/**
 * Returns the remote address and port of this socket as a {@code
 * SocketAddress} or null if the socket is not connected.
 *
 * @return the remote socket address and port.
 */
public SocketAddress getRemoteSocketAddress() {
  if (!isConnected()) {
    return null;
  }
  return new InetSocketAddress(getInetAddress(), getPort());
}

代码示例来源:origin: com.h2database/h2

@Override
public ArrayList<String> getClusterServers() {
  ArrayList<String> serverList = new ArrayList<>();
  for (Transfer transfer : transferList) {
    serverList.add(transfer.getSocket().getInetAddress().
        getHostAddress() + ":" +
        transfer.getSocket().getPort());
  }
  return serverList;
}

代码示例来源:origin: apache/activemq

/**
 * @return pretty print of 'this'
 */
@Override
public String toString() {
  return "" + (socket.isConnected() ? "tcp://" + socket.getInetAddress() + ":" + socket.getPort() + "@" + socket.getLocalPort()
      : (localLocation != null ? localLocation : remoteLocation)) ;
}

代码示例来源:origin: k9mail/k-9

@Override
  public Socket createSocket(Socket socket, String host, int port, String clientCertificateAlias)
      throws NoSuchAlgorithmException, KeyManagementException, MessagingException, IOException {

    TrustManager[] trustManagers = new TrustManager[] { new VeryTrustingTrustManager(serverCertificate) };

    SSLContext sslContext = SSLContext.getInstance("TLS");
    sslContext.init(null, trustManagers, null);

    SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
    return sslSocketFactory.createSocket(
        socket,
        socket.getInetAddress().getHostAddress(),
        socket.getPort(),
        true);
  }
}

代码示例来源:origin: apache/geode

public String getSocketString() {
 try {
  return String.valueOf(theSocket.getInetAddress()) + ':' +
    theSocket.getPort() + " timeout: " + theSocket.getSoTimeout();
 } catch (Exception e) {
  return String.format("Error in getSocketString: %s",
    e.getLocalizedMessage());
 }
}

代码示例来源:origin: mpetazzoni/ttorrent

@Override
public void onConnectionEstablished(SocketChannel socketChannel) throws IOException {
 this.myNext = new HandshakeReceiver(
     myContext,
     socketChannel.socket().getInetAddress().getHostAddress(),
     socketChannel.socket().getPort(),
     false);
}

代码示例来源:origin: apache/kafka

protected TransportLayer buildTransportLayer(String id, SelectionKey key, SocketChannel socketChannel) throws IOException {
  if (this.securityProtocol == SecurityProtocol.SASL_SSL) {
    return SslTransportLayer.create(id, key,
      sslFactory.createSslEngine(socketChannel.socket().getInetAddress().getHostName(), socketChannel.socket().getPort()));
  } else {
    return new PlaintextTransportLayer(key);
  }
}

代码示例来源:origin: apache/hbase

public SimpleServerRpcConnection(SimpleRpcServer rpcServer, SocketChannel channel,
  long lastContact) {
 super(rpcServer);
 this.channel = channel;
 this.lastContact = lastContact;
 this.data = null;
 this.dataLengthBuffer = ByteBuffer.allocate(4);
 this.socket = channel.socket();
 this.addr = socket.getInetAddress();
 if (addr == null) {
  this.hostAddress = "*Unknown*";
 } else {
  this.hostAddress = addr.getHostAddress();
 }
 this.remotePort = socket.getPort();
 if (rpcServer.socketSendBufferSize != 0) {
  try {
   socket.setSendBufferSize(rpcServer.socketSendBufferSize);
  } catch (IOException e) {
   SimpleRpcServer.LOG.warn(
    "Connection: unable to set socket send buffer size to " + rpcServer.socketSendBufferSize);
  }
 }
 this.responder = rpcServer.responder;
}

代码示例来源:origin: wildfly/wildfly

public SocketAddress getPeerAddress() {
  final Socket socket = conduit.getSocketChannel().socket();
  return new InetSocketAddress(socket.getInetAddress(), socket.getPort());
}

代码示例来源:origin: apache/ignite

/**
 * @param sock Socket.
 * @param clientNodeId Node ID.
 * @param log Logger.
 */
private ClientMessageWorker(Socket sock, UUID clientNodeId, IgniteLogger log) {
  super("tcp-disco-client-message-worker-[" + U.id8(clientNodeId)
    + ' ' + sock.getInetAddress().getHostAddress()
    + ":" + sock.getPort() + ']', log, Math.max(spi.metricsUpdateFreq, 10), null);
  this.sock = sock;
  this.clientNodeId = clientNodeId;
  lastMetricsUpdateMsgTime = U.currentTimeMillis();
}

代码示例来源:origin: apache/geode

/**
 * Return fake, temporary DistributedMember to represent the other vm this vm is connecting to
 *
 * @param sock the socket this handshake is operating on
 * @return temporary id to reprent the other vm
 */
private InternalDistributedMember getIDForSocket(Socket sock) {
 return new InternalDistributedMember(sock.getInetAddress(), sock.getPort(), false);
}

代码示例来源:origin: jamesdbloom/mockserver

public synchronized SSLSocket wrapSocket(Socket socket) throws IOException {
    // ssl socket factory
    javax.net.ssl.SSLSocketFactory sslSocketFactory = keyStoreFactory().sslContext().getSocketFactory();

    // ssl socket
    SSLSocket sslSocket = (SSLSocket) sslSocketFactory.createSocket(socket, socket.getInetAddress().getHostAddress(), socket.getPort(), true);
    sslSocket.setUseClientMode(true);
    sslSocket.startHandshake();
    return sslSocket;
  }
}

代码示例来源:origin: shyiko/mysql-binlog-connector-java

@Override
public SSLSocket createSocket(Socket socket) throws SocketException {
  SSLContext sc;
  try {
    sc = SSLContext.getInstance(this.protocol);
    initSSLContext(sc);
  } catch (GeneralSecurityException e) {
    throw new SocketException(e.getMessage());
  }
  try {
    return (SSLSocket) sc.getSocketFactory()
      .createSocket(socket, socket.getInetAddress().getHostName(), socket.getPort(), true);
  } catch (IOException e) {
    throw new SocketException(e.getMessage());
  }
}

代码示例来源:origin: org.apache.hadoop/hadoop-common

public Connection(SocketChannel channel, long lastContact) {
 this.channel = channel;
 this.lastContact = lastContact;
 this.data = null;
 
 // the buffer is initialized to read the "hrpc" and after that to read
 // the length of the Rpc-packet (i.e 4 bytes)
 this.dataLengthBuffer = ByteBuffer.allocate(4);
 this.unwrappedData = null;
 this.unwrappedDataLengthBuffer = ByteBuffer.allocate(4);
 this.socket = channel.socket();
 this.addr = socket.getInetAddress();
 if (addr == null) {
  this.hostAddress = "*Unknown*";
 } else {
  this.hostAddress = addr.getHostAddress();
 }
 this.remotePort = socket.getPort();
 this.responseQueue = new LinkedList<RpcCall>();
 if (socketSendBufferSize != 0) {
  try {
   socket.setSendBufferSize(socketSendBufferSize);
  } catch (IOException e) {
   LOG.warn("Connection: unable to set socket send buffer size to " +
        socketSendBufferSize);
  }
 }
}

相关文章