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

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

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

Socket.getRemoteSocketAddress介绍

[英]Returns the remote address and port of this socket as a SocketAddress or null if the socket is not connected.
[中]将此套接字的远程地址和端口作为SocketAddress返回,如果套接字未连接,则返回null。

代码示例

代码示例来源:origin: jenkinsci/jenkins

@Override
public void handle(Socket socket) throws IOException, InterruptedException {
  try {
    try (OutputStream stream = socket.getOutputStream()) {
      LOGGER.log(Level.FINE, "Received ping request from {0}", socket.getRemoteSocketAddress());
      stream.write(ping);
      stream.flush();
      LOGGER.log(Level.FINE, "Sent ping response to {0}", socket.getRemoteSocketAddress());
    }
  } finally {
    socket.close();
  }
}

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

/**
 * Used by "dump" 4-letter command to list all connection in
 * cnxnExpiryMap
 */
@Override
public String toString() {
  return "ip: " + sock.socket().getRemoteSocketAddress() +
      " sessionId: 0x" + Long.toHexString(sessionId);
}

代码示例来源:origin: jenkinsci/jenkins

public boolean connect(Socket socket) throws IOException {
    try {
      LOGGER.log(Level.FINE, "Requesting ping from {0}", socket.getRemoteSocketAddress());
      try (DataOutputStream out = new DataOutputStream(socket.getOutputStream())) {
        out.writeUTF("Protocol:Ping");
        try (InputStream in = socket.getInputStream()) {
          byte[] response = new byte[ping.length];
          int responseLength = in.read(response);
          if (responseLength == ping.length && Arrays.equals(response, ping)) {
            LOGGER.log(Level.FINE, "Received ping response from {0}", socket.getRemoteSocketAddress());
            return true;
          } else {
            LOGGER.log(Level.FINE, "Expected ping response from {0} of {1} got {2}", new Object[]{
                socket.getRemoteSocketAddress(),
                new String(ping, "UTF-8"),
                responseLength > 0 && responseLength <= response.length ?
                  new String(response, 0, responseLength, "UTF-8") :
                  "bad response length " + responseLength
            });
            return false;
          }
        }
      }
    } finally {
      socket.close();
    }
  }
}

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

@Override
public InetSocketAddress getRemoteSocketAddress() {
  if (sock.isOpen() == false) {
    return null;
  }
  return (InetSocketAddress) sock.socket().getRemoteSocketAddress();
}

代码示例来源:origin: jMonkeyEngine/jmonkeyengine

public SocketConnector( InetAddress address, int port ) throws IOException
{
  this.sock = new Socket(address, port);
  remoteAddress = sock.getRemoteSocketAddress(); // for info purposes 
  
  // Disable Nagle's buffering so data goes out when we
  // put it there.
  sock.setTcpNoDelay(true);
  
  in = sock.getInputStream();
  out = sock.getOutputStream();
  
  connected.set(true);
}

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

private void updateSocketAddresses() {
  Socket socket = ((SocketChannel) sockKey.channel()).socket();
  localSocketAddress = socket.getLocalSocketAddress();
  remoteSocketAddress = socket.getRemoteSocketAddress();
}

代码示例来源:origin: 4thline/cling

public static boolean isConnectionOpen(HttpServletRequest request, byte[] heartbeat) {
  Request jettyRequest = (Request)request;
  AbstractHttpConnection connection = jettyRequest.getConnection();
  Socket socket = (Socket)connection.getEndPoint().getTransport();
  if (log.isLoggable(Level.FINE))
    log.fine("Checking if client connection is still open: " + socket.getRemoteSocketAddress());
  try {
    socket.getOutputStream().write(heartbeat);
    socket.getOutputStream().flush();
    return true;
  } catch (IOException ex) {
    if (log.isLoggable(Level.FINE))
      log.fine("Client connection has been closed: " + socket.getRemoteSocketAddress());
    return false;
  }
}

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

@Override
protected SocketAddress remoteAddress0() {
  return javaChannel().socket().getRemoteSocketAddress();
}

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

public void init(String local_addr, String remote_addr, int local_port, int remote_port) throws Exception {
  local=new InetSocketAddress(local_addr, local_port);
  remote=new InetSocketAddress(remote_addr, remote_port);
  srv_sock=Util.createServerSocket(new DefaultSocketFactory(), "server", local.getAddress(), local.getPort(), local.getPort());
  System.out.println("Listening on " + srv_sock.getLocalSocketAddress());
  acceptor=new Acceptor();
  acceptor.start();
  sock=new Socket();
  //sock.bind(local);
  sock.setSendBufferSize(SOCK_SEND_BUF_SIZE);
  sock.setReceiveBufferSize(SOCK_RECV_BUF_SIZE);
  try {
    sock.connect(remote);
    output=new DataOutputStream(new BufferedOutputStream(sock.getOutputStream()));
    System.out.println("Connected to " + sock.getRemoteSocketAddress());
  }
  catch(Throwable t) {
    System.out.println("Failed connecting to " + remote + ": will only act as server");
  }
}

代码示例来源:origin: EsotericSoftware/kryonet

/** Returns the IP address and port of the remote end of the TCP connection, or null if this connection is not connected. */
public InetSocketAddress getRemoteAddressTCP () {
  SocketChannel socketChannel = tcp.socketChannel;
  if (socketChannel != null) {
    Socket socket = tcp.socketChannel.socket();
    if (socket != null) {
      return (InetSocketAddress)socket.getRemoteSocketAddress();
    }
  }
  return null;
}

代码示例来源:origin: jenkinsci/jenkins

@Override
public void run() {
  try {
    LOGGER.log(Level.FINE, "Accepted connection #{0} from {1}", new Object[] {id, s.getRemoteSocketAddress()});
        new BufferedWriter(new OutputStreamWriter(s.getOutputStream(),"UTF-8")),
        true); // DEPRECATED: newer protocol shouldn't use PrintWriter but should use DataOutputStream
      if (p!=null) {
        if (Jenkins.getInstance().getAgentProtocols().contains(protocol)) {
          LOGGER.log(p instanceof PingAgentProtocol ? Level.FINE : Level.INFO, "Accepted {0} connection #{1} from {2}", new Object[] {protocol, id, this.s.getRemoteSocketAddress()});
          p.handle(this.s);
        } else {

代码示例来源:origin: io.netty/netty

@Override
InetSocketAddress getRemoteSocketAddress() throws Exception {
  return (InetSocketAddress) channel.socket().getRemoteSocketAddress();
}

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

public void init(String local_addr, String remote_addr, int local_port, int remote_port) throws Exception {
  local=new InetSocketAddress(local_addr, local_port);
  remote=new InetSocketAddress(remote_addr, remote_port);
  destination=new IpAddress(remote.getAddress(), remote.getPort());
  srv_sock=Util.createServerSocket(new DefaultSocketFactory(), "server", local.getAddress(), local.getPort());
  System.out.println("Listening on " + srv_sock.getLocalSocketAddress());
  acceptor=new Acceptor();
  acceptor.start();
  sock=new Socket();
  //sock.bind(local);
  sock.setSendBufferSize(SOCK_SEND_BUF_SIZE);
  sock.setReceiveBufferSize(SOCK_RECV_BUF_SIZE);
  try {
    sock.connect(remote);
    output=new DataOutputStream(new BufferedOutputStream(sock.getOutputStream()));
    System.out.println("Connected to " + sock.getRemoteSocketAddress());
  }
  catch(Throwable t) {
    System.out.println("Failed connecting to " + remote + ": will only act as server");
  }
}

代码示例来源:origin: jMonkeyEngine/jmonkeyengine

public String getAddress()
{
  return String.valueOf(socket.socket().getRemoteSocketAddress()); 
}

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

@Override
public void receiveFlowFiles(final Socket socket) throws IOException {
  final InputStream in = new BufferedInputStream(socket.getInputStream());
  final OutputStream out = new BufferedOutputStream(socket.getOutputStream());
  String peerDescription = socket.getInetAddress().getHostName();
  if (socket instanceof SSLSocket) {
    logger.debug("Connection received from peer {}", peerDescription);
    peerDescription = authorizer.authorize((SSLSocket) socket);
    logger.debug("Client Identities are authorized to load balance data for peer {}", peerDescription);
  }
  final int version = negotiateProtocolVersion(in, out, peerDescription);
  if (version == SOCKET_CLOSED) {
    socket.close();
    return;
  }
  if (version == NO_DATA_AVAILABLE) {
    logger.debug("No data is available from {}", socket.getRemoteSocketAddress());
    return;
  }
  receiveFlowFiles(in, out, peerDescription, version);
}

代码示例来源:origin: org.apache.zookeeper/zookeeper

@Override
public InetSocketAddress getRemoteSocketAddress() {
  if (sock.isOpen() == false) {
    return null;
  }
  return (InetSocketAddress) sock.socket().getRemoteSocketAddress();
}

代码示例来源:origin: Red5/red5-server

ServerSocket serverSocket = new ServerSocket(port); 
  Socket clientSocket = serverSocket.accept(); 
  PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true); 
  BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
 ) {
log.info("Connected - local: {} remote: {}", clientSocket.getLocalSocketAddress(), clientSocket.getRemoteSocketAddress());
String inputLine = in.readLine();
if (inputLine != null && token.equals(inputLine)) {

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

@Override
protected SocketAddress remoteAddress0() {
  return javaChannel().socket().getRemoteSocketAddress();
}

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

sock.setReceiveBufferSize(receivebuf);
sock.setSendBufferSize(sendbuf);
DataOutputStream out=new DataOutputStream(new BufferedOutputStream(sock.getOutputStream()));
byte[] buf=new byte[size];
int cnt=1;
client_sock.setReceiveBufferSize(receivebuf);
client_sock.setSendBufferSize(sendbuf);
System.out.println("-- accepted connection from " + client_sock.getRemoteSocketAddress());
DataInputStream in=new DataInputStream(new BufferedInputStream(client_sock.getInputStream()));
byte[] buf=new byte[size];

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

@Override
public synchronized InetSocketAddress getAddress() {
  if (key == null) return null;
  SocketChannel socketChannel = (SocketChannel) key.channel();
  SocketAddress addr = socketChannel.socket().getRemoteSocketAddress();
  if (addr instanceof InetSocketAddress) {
    InetSocketAddress address = (InetSocketAddress) addr;
    return address;
  } else {
    throw new IllegalStateException("Cannot get remote address!");
  }
}

相关文章