Java Code Examples for java.net.Socket.getLocalAddress()

The following are Jave code examples for showing how to use getLocalAddress() of the java.net.Socket class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: T0rlib4j   File: SocksSocket.java   Source Code and License Vote up 6 votes
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);
	}
}
 
Example 2
Project: T0rlib4Android   File: SocksSocket.java   Source Code and License Vote up 5 votes
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);
	}
}
 
Example 3
Project: lighthouse   File: BHttpConnectionBase.java   Source Code and License Vote up 5 votes
@Override
public InetAddress getLocalAddress() {
    final Socket socket = this.socketHolder.get();
    return socket != null ? socket.getLocalAddress() : null;
}
 
Example 4
Project: GitHub   File: SocksProxy.java   Source Code and License Vote up 4 votes
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);
  }
}
 
Example 5
Project: GitHub   File: SocksProxy.java   Source Code and License Vote up 4 votes
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);
  }
}
 
Example 6
Project: java-android-websocket-client   File: BHttpConnectionBase.java   Source Code and License Vote up 4 votes
@Override
public InetAddress getLocalAddress() {
    final Socket socket = this.socketHolder.get();
    return socket != null ? socket.getLocalAddress() : null;
}