本文整理了Java中java.net.Socket.bind()
方法的一些代码示例,展示了Socket.bind()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Socket.bind()
方法的具体详情如下:
包路径:java.net.Socket
类名称:Socket
方法名:bind
[英]Binds this socket to the given local host address and port specified by the SocketAddress localAddr. If localAddr is set to null, this socket will be bound to an available local address on any free port.
[中]将此套接字绑定到SocketAddress localAddr指定的给定本地主机地址和端口。如果localAddr设置为null,则此套接字将绑定到任何可用端口上的可用本地地址。
代码示例来源:origin: stackoverflow.com
Socket s = new Socket();
s.bind(new InetSocketAddress("172.16.1.102", 5000));
s.connect(new InetSocketAddress("google.com", 80));
代码示例来源:origin: apache/activemq
@Override
public Socket createSocket(String address, int port, InetAddress localAddresss, int localPort) throws IOException, UnknownHostException {
SocketChannel channel = SocketChannel.open();
channel.socket().bind(new InetSocketAddress(localAddresss, localPort));
channel.connect(new InetSocketAddress(address, port));
return channel.socket();
}
代码示例来源:origin: org.apache.hadoop/hadoop-common
"Local address %s must be of same family as remote address %s.",
localAddr, endpoint);
socket.bind(localAddr);
if (ch == null) {
socket.connect(endpoint, timeout);
} else {
SocketIOWithTimeout.connect(ch, endpoint, timeout);
代码示例来源:origin: org.apache.hadoop/hadoop-common
@Override
public Socket createSocket(String host, int port,
InetAddress localHostAddr, int localPort) throws IOException,
UnknownHostException {
Socket socket = createSocket();
socket.bind(new InetSocketAddress(localHostAddr, localPort));
socket.connect(new InetSocketAddress(host, port));
return socket;
}
代码示例来源:origin: apache/activemq
@Override
public Socket createSocket(InetAddress address, int port, InetAddress localAddresss, int localPort) throws IOException {
SocketChannel channel = SocketChannel.open();
channel.socket().bind(new InetSocketAddress(localAddresss, localPort));
channel.connect(new InetSocketAddress(address, port));
return channel.socket();
}
};
代码示例来源:origin: stackoverflow.com
delegate.bind(localAddr);
delegate.connect(remoteAddr);
delegate.connect(remoteAddr, timeout);
代码示例来源:origin: org.apache.hadoop/hadoop-common
@Override
public Socket createSocket(String host, int port,
InetAddress localHostAddr, int localPort) throws IOException,
UnknownHostException {
Socket socket = createSocket();
socket.bind(new InetSocketAddress(localHostAddr, localPort));
socket.connect(new InetSocketAddress(host, port));
return socket;
}
代码示例来源:origin: Qihoo360/XLearning
public static void getReservePort(Socket socket, String localHost, int reservePortBegin, int reservePortEnd) throws IOException {
int i = 0;
Random random = new Random(System.currentTimeMillis());
while (i < 1000) {
int rand = random.nextInt(reservePortEnd - reservePortBegin);
try {
socket.bind(new InetSocketAddress(localHost, reservePortBegin + rand));
return;
} catch (IOException e) {
try {
Thread.sleep(1000);
} catch (InterruptedException e2) {}
}
}
throw new IOException("couldn't allocate a unused port");
}
代码示例来源:origin: stackoverflow.com
delegate.bind(localAddr);
delegate.connect(remoteAddr);
delegate.connect(remoteAddr, timeout);
代码示例来源:origin: apache/nifi
@Override
public Socket createSocket(InetAddress addr, int port, InetAddress localHostAddr, int localPort) throws IOException {
Socket socket = createSocket();
socket.bind(new InetSocketAddress(localHostAddr, localPort));
socket.connect(new InetSocketAddress(addr, port));
return socket;
}
代码示例来源:origin: spotify/helios
s.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), port));
} catch (IOException e) {
return false;
代码示例来源:origin: stackoverflow.com
delegate.bind(localAddr);
delegate.connect(remoteAddr);
delegate.connect(remoteAddr, timeout);
代码示例来源:origin: internetarchive/heritrix3
public Socket createSocket(String host, int port,
InetAddress localHost, int localPort) throws IOException,
UnknownHostException {
Socket sock = createSocket();
sock.bind(new InetSocketAddress(localHost, localPort));
sock.connect(new InetSocketAddress(host, port), connectTimeoutMs);
return sock;
}
代码示例来源:origin: apache/ignite
/**
* Creates socket binding it to a local host address. This operation is not blocking.
*
* @return Created socket.
* @throws IOException If failed.
*/
Socket createSocket() throws IOException {
Socket sock = null;
try {
if (isSslEnabled())
sock = sslSockFactory.createSocket();
else
sock = new Socket();
sock.bind(new InetSocketAddress(locHost, 0));
sock.setTcpNoDelay(true);
return sock;
} catch (IOException e) {
if (sock != null)
U.closeQuiet(sock);
throw e;
}
}
代码示例来源:origin: org.apache.hadoop/hadoop-common
@Override
public Socket createSocket(InetAddress addr, int port,
InetAddress localHostAddr, int localPort) throws IOException {
Socket socket = createSocket();
socket.bind(new InetSocketAddress(localHostAddr, localPort));
socket.connect(new InetSocketAddress(addr, port));
return socket;
}
代码示例来源:origin: wildfly/wildfly
public static void bind(Socket sock, InetAddress bind_addr, int start_port, int end_port) throws Exception {
int original_start_port=start_port;
while(true) {
try {
InetSocketAddress sock_addr=new InetSocketAddress(bind_addr, start_port);
sock.bind(sock_addr);
}
catch(SocketException bind_ex) {
if(start_port == end_port)
throw new BindException("No available port to bind to in range [" + original_start_port + " .. " + end_port + "]");
if(bind_addr != null && !bind_addr.isLoopbackAddress()) {
NetworkInterface nic=NetworkInterface.getByInetAddress(bind_addr);
if(nic == null)
throw new BindException("bind_addr " + bind_addr + " is not a valid interface: " + bind_ex);
}
start_port++;
continue;
}
break;
}
}
代码示例来源:origin: internetarchive/heritrix3
public Socket createSocket(InetAddress address, int port,
InetAddress localAddress, int localPort) throws IOException {
Socket sock = createSocket();
sock.bind(new InetSocketAddress(localAddress, localPort));
sock.connect(new InetSocketAddress(address, port), connectTimeoutMs);
return sock;
}
代码示例来源:origin: jphp-group/jphp
@Signature({@Arg("hostname"), @Arg("port")})
public Memory bind(Environment env, Memory... args) throws IOException {
socket.bind(new InetSocketAddress(args[0].toString(), args[1].toInteger()));
return Memory.NULL;
}
代码示例来源:origin: org.apache.hadoop/hadoop-common
@Override
public Socket createSocket(InetAddress addr, int port,
InetAddress localHostAddr, int localPort) throws IOException {
Socket socket = createSocket();
socket.bind(new InetSocketAddress(localHostAddr, localPort));
socket.connect(new InetSocketAddress(addr, port));
return socket;
}
代码示例来源:origin: wildfly/wildfly
protected void connect(Address dest, boolean send_local_addr) throws Exception {
SocketAddress destAddr=new InetSocketAddress(((IpAddress)dest).getIpAddress(), ((IpAddress)dest).getPort());
try {
if(!server.defer_client_binding)
this.sock.bind(new InetSocketAddress(server.client_bind_addr, server.client_bind_port));
Util.connect(this.sock, destAddr, server.sock_conn_timeout);
if(this.sock.getLocalSocketAddress() != null && this.sock.getLocalSocketAddress().equals(destAddr))
throw new IllegalStateException("socket's bind and connect address are the same: " + destAddr);
this.out=new DataOutputStream(createBufferedOutputStream(sock.getOutputStream()));
this.in=new DataInputStream(createBufferedInputStream(sock.getInputStream()));
connected=sock.isConnected();
if(send_local_addr)
sendLocalAddress(server.localAddress());
}
catch(Exception t) {
Util.close(this.sock);
connected=false;
throw t;
}
}
内容来源于网络,如有侵权,请联系作者删除!