1 16 package org.apache.commons.net; 17 18 import java.io.IOException ; 19 import java.io.InputStream ; 20 import java.io.OutputStream ; 21 import java.net.InetAddress ; 22 import java.net.Socket ; 23 import java.net.SocketException ; 24 25 46 public abstract class SocketClient 47 { 48 52 public static final String NETASCII_EOL = "\r\n"; 53 54 55 private static final SocketFactory __DEFAULT_SOCKET_FACTORY = 56 new DefaultSocketFactory(); 57 58 59 protected int _timeout_; 60 61 62 protected Socket _socket_; 63 64 67 protected boolean _isConnected_; 68 69 70 protected int _defaultPort_; 71 72 73 protected InputStream _input_; 74 75 76 protected OutputStream _output_; 77 78 79 protected SocketFactory _socketFactory_; 80 81 82 88 public SocketClient() 89 { 90 _socket_ = null; 91 _input_ = null; 92 _output_ = null; 93 _timeout_ = 0; 94 _defaultPort_ = 0; 95 _isConnected_ = false; 96 _socketFactory_ = __DEFAULT_SOCKET_FACTORY; 97 } 98 99 100 116 protected void _connectAction_() throws IOException 117 { 118 _socket_.setSoTimeout(_timeout_); 119 _input_ = _socket_.getInputStream(); 120 _output_ = _socket_.getOutputStream(); 121 _isConnected_ = true; 122 } 123 124 125 138 public void connect(InetAddress host, int port) 139 throws SocketException , IOException 140 { 141 _socket_ = _socketFactory_.createSocket(host, port); 142 _connectAction_(); 143 } 144 145 159 public void connect(String hostname, int port) 160 throws SocketException , IOException 161 { 162 _socket_ = _socketFactory_.createSocket(hostname, port); 163 _connectAction_(); 164 } 165 166 167 182 public void connect(InetAddress host, int port, 183 InetAddress localAddr, int localPort) 184 throws SocketException , IOException 185 { 186 _socket_ = _socketFactory_.createSocket(host, port, localAddr, localPort); 187 _connectAction_(); 188 } 189 190 191 207 public void connect(String hostname, int port, 208 InetAddress localAddr, int localPort) 209 throws SocketException , IOException 210 { 211 _socket_ = 212 _socketFactory_.createSocket(hostname, port, localAddr, localPort); 213 _connectAction_(); 214 } 215 216 217 229 public void connect(InetAddress host) throws SocketException , IOException 230 { 231 connect(host, _defaultPort_); 232 } 233 234 235 248 public void connect(String hostname) throws SocketException , IOException 249 { 250 connect(hostname, _defaultPort_); 251 } 252 253 254 264 public void disconnect() throws IOException 265 { 266 _socket_.close(); 267 _input_.close(); 268 _output_.close(); 269 _socket_ = null; 270 _input_ = null; 271 _output_ = null; 272 _isConnected_ = false; 273 } 274 275 276 282 public boolean isConnected() 283 { 284 return _isConnected_; 285 } 286 287 288 296 public void setDefaultPort(int port) 297 { 298 _defaultPort_ = port; 299 } 300 301 307 public int getDefaultPort() 308 { 309 return _defaultPort_; 310 } 311 312 313 324 public void setDefaultTimeout(int timeout) 325 { 326 _timeout_ = timeout; 327 } 328 329 330 337 public int getDefaultTimeout() 338 { 339 return _timeout_; 340 } 341 342 343 352 public void setSoTimeout(int timeout) throws SocketException 353 { 354 _socket_.setSoTimeout(timeout); 355 } 356 357 358 364 public int getSoTimeout() throws SocketException 365 { 366 return _socket_.getSoTimeout(); 367 } 368 369 376 public void setTcpNoDelay(boolean on) throws SocketException 377 { 378 _socket_.setTcpNoDelay(on); 379 } 380 381 382 390 public boolean getTcpNoDelay() throws SocketException 391 { 392 return _socket_.getTcpNoDelay(); 393 } 394 395 396 403 public void setSoLinger(boolean on, int val) throws SocketException 404 { 405 _socket_.setSoLinger(on, val); 406 } 407 408 409 416 public int getSoLinger() throws SocketException 417 { 418 return _socket_.getSoLinger(); 419 } 420 421 422 429 public int getLocalPort() 430 { 431 return _socket_.getLocalPort(); 432 } 433 434 435 440 public InetAddress getLocalAddress() 441 { 442 return _socket_.getLocalAddress(); 443 } 444 445 452 public int getRemotePort() 453 { 454 return _socket_.getPort(); 455 } 456 457 458 461 public InetAddress getRemoteAddress() 462 { 463 return _socket_.getInetAddress(); 464 } 465 466 467 476 public boolean verifyRemote(Socket socket) 477 { 478 InetAddress host1, host2; 479 480 host1 = socket.getInetAddress(); 481 host2 = getRemoteAddress(); 482 483 return host1.equals(host2); 484 } 485 486 487 495 public void setSocketFactory(SocketFactory factory) 496 { 497 if (factory == null) 498 _socketFactory_ = __DEFAULT_SOCKET_FACTORY; 499 else 500 _socketFactory_ = factory; 501 } 502 } 503 504 505 | Popular Tags |