Changeset 1252
- Timestamp:
- Oct 22, 2010, 10:45:07 AM (13 years ago)
- Location:
- extensions/net.sf.basedb.torrent/trunk/src/external/hpbtc/hpbtc/protocol
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
extensions/net.sf.basedb.torrent/trunk/src/external/hpbtc/hpbtc/protocol/network/NetworkReader.java
r1250 r1252 13 13 import java.net.InetSocketAddress; 14 14 import java.net.ServerSocket; 15 import java.net.SocketAddress; 15 16 import java.nio.channels.ClosedChannelException; 16 17 import java.nio.channels.SelectableChannel; … … 38 39 39 40 public void connect(final int port) throws IOException { 41 connect(new InetSocketAddress(InetAddress.getLocalHost(), port)); 42 } 43 44 /** 45 Connect the reader to the local address and port. 46 @param address The local network address to bind to, or null to let the system 47 decide 48 @return The port bound to 49 */ 50 public InetSocketAddress connect(final InetSocketAddress address) 51 throws IOException 52 { 40 53 serverCh = ServerSocketChannel.open(); 41 serverCh.socket().bind(new InetSocketAddress(42 InetAddress.getLocalHost(), port));54 ServerSocket socket = serverCh.socket(); 55 socket.bind(address); 43 56 serverCh.configureBlocking(false); 44 57 super.connect(); 58 return (InetSocketAddress)socket.getLocalSocketAddress(); 45 59 } 46 60 47 61 @Override 48 62 protected void registerSelectors(Selector selector) … … 55 69 @Override 56 70 public int connect() throws IOException { 57 serverCh = ServerSocketChannel.open(); 58 ServerSocket s = serverCh.socket(); 59 s.bind(null); 60 serverCh.configureBlocking(false); 61 super.connect(); 62 return s.getLocalPort(); 71 return connect(new InetSocketAddress(InetAddress.getLocalHost(), 0)).getPort(); 63 72 } 64 73 -
extensions/net.sf.basedb.torrent/trunk/src/external/hpbtc/hpbtc/protocol/processor/Client.java
r1249 r1252 15 15 import hpbtc.util.TorrentUtil; 16 16 import java.net.InetAddress; 17 import java.net.InetSocketAddress; 18 import java.net.SocketAddress; 17 19 import java.util.logging.Logger; 18 20 … … 76 78 } 77 79 80 public InetSocketAddress startProtocol(InetSocketAddress address) 81 throws IOException 82 { 83 initNetwork(); 84 InetSocketAddress connected = netReader.connect(address); 85 this.port = connected.getPort(); 86 netWriter.connect(); 87 logger.fine("Started client on " + connected); 88 return connected; 89 } 90 78 91 public void startProtocol(int port) throws IOException { 79 this.port = port; 80 initNetwork(); 81 netReader.connect(port); 82 netWriter.connect(); 83 logger.fine("Started client on " + InetAddress.getLocalHost() + ":" + port); 92 startProtocol(new InetSocketAddress(InetAddress.getLocalHost(), port)); 84 93 } 85 94 86 95 public int startProtocol() throws IOException { 87 initNetwork(); 88 port = netReader.connect(); 89 netWriter.connect(); 90 logger.fine("Started client on " + InetAddress.getLocalHost() + ":" + port); 91 return port; 96 return startProtocol(new InetSocketAddress(InetAddress.getLocalHost(), 0)).getPort(); 92 97 } 93 98
Note: See TracChangeset
for help on using the changeset viewer.