当前位置: 首页>>代码示例>>Java>>正文


Java Socket.getLocalAddress方法代码示例

本文整理汇总了Java中java.net.Socket.getLocalAddress方法的典型用法代码示例。如果您正苦于以下问题:Java Socket.getLocalAddress方法的具体用法?Java Socket.getLocalAddress怎么用?Java Socket.getLocalAddress使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.net.Socket的用法示例。


在下文中一共展示了Socket.getLocalAddress方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: doDirect

import java.net.Socket; //导入方法依赖的package包/类
private void doDirect() throws SocksException {
	try {
		log.debug("IP: {}_{}", remoteIP, remotePort);
		directSock = new Socket(remoteIP, remotePort);
		proxy.out = directSock.getOutputStream();
		proxy.in = directSock.getInputStream();
		proxy.proxySocket = directSock;
		localIP = directSock.getLocalAddress();
		localPort = directSock.getLocalPort();
	} catch (final IOException io_ex) {
		final int errCode = SocksProxyBase.SOCKS_DIRECT_FAILED;
		throw new SocksException(errCode, "Direct connect failed:", io_ex);
	}
}
 
开发者ID:PanagiotisDrakatos,项目名称:T0rlib4j,代码行数:15,代码来源:SocksSocket.java

示例2: acceptCommand

import java.net.Socket; //导入方法依赖的package包/类
private void acceptCommand(InetAddress fromAddress, BufferedSource fromSource,
    BufferedSink fromSink) throws IOException {
  // Read the command.
  int version = fromSource.readByte() & 0xff;
  if (version != VERSION_5) throw new ProtocolException("unexpected version: " + version);
  int command = fromSource.readByte() & 0xff;
  int reserved = fromSource.readByte() & 0xff;
  if (reserved != 0) throw new ProtocolException("unexpected reserved: " + reserved);

  int addressType = fromSource.readByte() & 0xff;
  InetAddress toAddress;
  switch (addressType) {
    case ADDRESS_TYPE_IPV4:
      toAddress = InetAddress.getByAddress(fromSource.readByteArray(4L));
      break;

    case ADDRESS_TYPE_DOMAIN_NAME:
      int domainNameLength = fromSource.readByte() & 0xff;
      String domainName = fromSource.readUtf8(domainNameLength);
      // Resolve HOSTNAME_THAT_ONLY_THE_PROXY_KNOWS to localhost.
      toAddress = domainName.equalsIgnoreCase(HOSTNAME_THAT_ONLY_THE_PROXY_KNOWS)
          ? InetAddress.getByName("localhost")
          : InetAddress.getByName(domainName);
      break;

    default:
      throw new ProtocolException("unsupported address type: " + addressType);
  }

  int port = fromSource.readShort() & 0xffff;

  switch (command) {
    case COMMAND_CONNECT:
      Socket toSocket = new Socket(toAddress, port);
      byte[] localAddress = toSocket.getLocalAddress().getAddress();
      if (localAddress.length != 4) {
        throw new ProtocolException("unexpected address: " + toSocket.getLocalAddress());
      }

      // Write the reply.
      fromSink.writeByte(VERSION_5);
      fromSink.writeByte(REPLY_SUCCEEDED);
      fromSink.writeByte(0);
      fromSink.writeByte(ADDRESS_TYPE_IPV4);
      fromSink.write(localAddress);
      fromSink.writeShort(toSocket.getLocalPort());
      fromSink.emit();

      logger.log(Level.INFO, "SocksProxy connected " + fromAddress + " to " + toAddress);

      // Copy sources to sinks in both directions.
      BufferedSource toSource = Okio.buffer(Okio.source(toSocket));
      BufferedSink toSink = Okio.buffer(Okio.sink(toSocket));
      transfer(fromAddress, toAddress, fromSource, toSink);
      transfer(fromAddress, toAddress, toSource, fromSink);
      break;

    default:
      throw new ProtocolException("unexpected command: " + command);
  }
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:62,代码来源:SocksProxy.java

示例3: acceptCommand

import java.net.Socket; //导入方法依赖的package包/类
private void acceptCommand(InetAddress fromAddress, BufferedSource fromSource,
    BufferedSink fromSink) throws IOException {
  // Read the command.
  int version = fromSource.readByte() & 0xff;
  if (version != VERSION_5) throw new ProtocolException("unexpected version: " + version);
  int command = fromSource.readByte() & 0xff;
  int reserved = fromSource.readByte() & 0xff;
  if (reserved != 0) throw new ProtocolException("unexpected reserved: " + reserved);

  int addressType = fromSource.readByte() & 0xff;
  InetAddress toAddress;
  switch (addressType) {
    case ADDRESS_TYPE_IPV4:
      toAddress = InetAddress.getByAddress(fromSource.readByteArray(4L));
      break;

    case ADDRESS_TYPE_DOMAIN_NAME:
      int domainNameLength = fromSource.readByte() & 0xff;
      String domainName = fromSource.readUtf8(domainNameLength);
      // Resolve HOSTNAME_THAT_ONLY_THE_PROXY_KNOWS to localhost.
      toAddress = domainName.equalsIgnoreCase(HOSTNAME_THAT_ONLY_THE_PROXY_KNOWS)
          ? InetAddress.getByName("localhost")
          : InetAddress.getByName(domainName);
      break;

    default:
      throw new ProtocolException("unsupported address type: " + addressType);
  }

  int port = fromSource.readShort() & 0xffff;

  switch (command) {
    case COMMAND_CONNECT:
      Socket toSocket = new Socket(toAddress, port);
      byte[] localAddress = toSocket.getLocalAddress().getAddress();
      if (localAddress.length != 4) {
        throw new ProtocolException("unexpected address: " + toSocket.getLocalAddress());
      }

      // Write the reply.
      fromSink.writeByte(VERSION_5);
      fromSink.writeByte(REPLY_SUCCEEDED);
      fromSink.writeByte(0);
      fromSink.writeByte(ADDRESS_TYPE_IPV4);
      fromSink.write(localAddress);
      fromSink.writeShort(toSocket.getLocalPort());
      fromSink.emit();

      logger.log(Level.INFO, "SocksProxy connected " + fromAddress + " to " + toAddress);

      // Copy sources to sinks in both directions.
      BufferedSource toSource = Okio.buffer(Okio.source(toSocket));
      BufferedSink toSink = Okio.buffer(Okio.sink(toSocket));
      openSockets.add(toSocket);

      transfer(fromAddress, toAddress, fromSource, toSink);
      transfer(fromAddress, toAddress, toSource, fromSink);
      break;

    default:
      throw new ProtocolException("unexpected command: " + command);
  }
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:64,代码来源:SocksProxy.java

示例4: getLocalAddress

import java.net.Socket; //导入方法依赖的package包/类
@Override
public InetAddress getLocalAddress() {
    final Socket socket = this.socketHolder.get();
    return socket != null ? socket.getLocalAddress() : null;
}
 
开发者ID:gusavila92,项目名称:java-android-websocket-client,代码行数:6,代码来源:BHttpConnectionBase.java


注:本文中的java.net.Socket.getLocalAddress方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。