本文整理汇总了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);
}
}
示例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);
}
}
示例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);
}
}
示例4: getLocalAddress
import java.net.Socket; //导入方法依赖的package包/类
@Override
public InetAddress getLocalAddress() {
final Socket socket = this.socketHolder.get();
return socket != null ? socket.getLocalAddress() : null;
}