TCPConnection(Address peer_addr) throws Exception { if (peer_addr == null) throw new IllegalArgumentException("Invalid parameter peer_addr=" + peer_addr); SocketAddress destAddr = new InetSocketAddress( ((IpAddress) peer_addr).getIpAddress(), ((IpAddress) peer_addr).getPort()); this.sock = new Socket(); this.sock.bind(new InetSocketAddress(bind_addr, 0)); Util.connect(this.sock, destAddr, sock_conn_timeout); setSocketParameters(sock); this.out = new DataOutputStream(new BufferedOutputStream(sock.getOutputStream())); this.in = new DataInputStream(new BufferedInputStream(sock.getInputStream())); sendLocalAddress(getLocalAddress()); this.peer_addr = peer_addr; }
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)); if (this.sock.getLocalSocketAddress() != null && this.sock.getLocalSocketAddress().equals(destAddr)) throw new IllegalStateException( "socket's bind and connect address are the same: " + destAddr); Util.connect(this.sock, destAddr, server.sock_conn_timeout); this.out = new DataOutputStream(new BufferedOutputStream(sock.getOutputStream())); this.in = new DataInputStream(new BufferedInputStream(sock.getInputStream())); if (send_local_addr) sendLocalAddress(server.localAddress()); } catch (Exception t) { Util.close(this.sock); throw t; } }