1 16 package org.apache.commons.net; 17 18 import java.net.DatagramSocket ; 19 import java.net.InetAddress ; 20 import java.net.SocketException ; 21 22 47 48 public abstract class DatagramSocketClient 49 { 50 54 private static final DatagramSocketFactory __DEFAULT_SOCKET_FACTORY = 55 new DefaultDatagramSocketFactory(); 56 57 58 protected int _timeout_; 59 60 61 protected DatagramSocket _socket_; 62 63 66 protected boolean _isOpen_; 67 68 69 protected DatagramSocketFactory _socketFactory_; 70 71 75 public DatagramSocketClient() 76 { 77 _socket_ = null; 78 _timeout_ = 0; 79 _isOpen_ = false; 80 _socketFactory_ = __DEFAULT_SOCKET_FACTORY; 81 } 82 83 84 95 public void open() throws SocketException 96 { 97 _socket_ = _socketFactory_.createDatagramSocket(); 98 _socket_.setSoTimeout(_timeout_); 99 _isOpen_ = true; 100 } 101 102 103 115 public void open(int port) throws SocketException 116 { 117 _socket_ = _socketFactory_.createDatagramSocket(port); 118 _socket_.setSoTimeout(_timeout_); 119 _isOpen_ = true; 120 } 121 122 123 137 public void open(int port, InetAddress laddr) throws SocketException 138 { 139 _socket_ = _socketFactory_.createDatagramSocket(port, laddr); 140 _socket_.setSoTimeout(_timeout_); 141 _isOpen_ = true; 142 } 143 144 145 146 154 public void close() 155 { 156 _socket_.close(); 157 _socket_ = null; 158 _isOpen_ = false; 159 } 160 161 162 167 public boolean isOpen() 168 { 169 return _isOpen_; 170 } 171 172 173 184 public void setDefaultTimeout(int timeout) 185 { 186 _timeout_ = timeout; 187 } 188 189 190 197 public int getDefaultTimeout() 198 { 199 return _timeout_; 200 } 201 202 203 211 public void setSoTimeout(int timeout) throws SocketException 212 { 213 _socket_.setSoTimeout(timeout); 214 } 215 216 217 224 public int getSoTimeout() throws SocketException 225 { 226 return _socket_.getSoTimeout(); 227 } 228 229 230 238 public int getLocalPort() 239 { 240 return _socket_.getLocalPort(); 241 } 242 243 244 251 public InetAddress getLocalAddress() 252 { 253 return _socket_.getLocalAddress(); 254 } 255 256 257 266 public void setDatagramSocketFactory(DatagramSocketFactory factory) 267 { 268 if (factory == null) 269 _socketFactory_ = __DEFAULT_SOCKET_FACTORY; 270 else 271 _socketFactory_ = factory; 272 } 273 } 274 | Popular Tags |