KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > dyade > aaa > util > SocketAddress


1 /*
2  * Copyright (C) 2004 - France Telecom R&D
3  * Copyright (C) 2004 - ScalAgent Distributed Technologies
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18  * USA.
19  *
20  * Initial developer(s): ScalAgent Distributed Technologies
21  * Contributor(s):
22  */

23 package fr.dyade.aaa.util;
24
25 import java.net.InetAddress JavaDoc;
26 import java.net.UnknownHostException JavaDoc;
27
28 /**
29  * This class implements an IP Socket Address (IP address + port number)
30  */

31 public class SocketAddress {
32   /** The hostname of the Socket Address */
33   private String JavaDoc hostname;
34   /** The IP address of the Socket Address */
35   private InetAddress JavaDoc addr;
36   /** The port number of the Socket Address */
37   private int port;
38
39   /**
40    * Creates a socket address from a hostname and a port number.
41    *
42    * @param hostname the Host name
43    * @param port The port number
44    */

45   public SocketAddress(String JavaDoc hostname, int port) {
46     this.hostname = hostname;
47     try {
48       addr = InetAddress.getByName(this.hostname);
49     } catch (UnknownHostException JavaDoc exc) {
50       addr = null;
51     }
52     this.port = port;
53   }
54
55   /**
56    * Resolves the IP address for this hostname, don't use an eventually
57    * caching address.
58    *
59    * @return the resolved IP address.
60    */

61   public InetAddress JavaDoc resetAddr() {
62     try {
63       addr = InetAddress.getByName(getHostname());
64     } catch (UnknownHostException JavaDoc exc) {
65       addr = null;
66     }
67     return addr;
68   }
69
70   /**
71    * Gets the port number.
72    *
73    * @return the port number.
74    */

75    public int getPort() {
76     return port;
77   }
78   
79   /**
80    * Gets the <code>hostname</code>.
81    *
82    * @return the hostname part of the address.
83    */

84   public String JavaDoc getHostname() {
85     return hostname;
86   }
87   
88   /**
89    * Gets the <code>InetAddress</code>.
90    *
91    * @return the InetAdress or <code>null</code> if it is unresolved.
92    */

93   public InetAddress JavaDoc getAddress() {
94     return addr;
95   }
96   
97   /**
98    * Compares this object against the specified object.
99    *
100    * @param obj the object to compare against.
101    * @return <code>true</code> if the objects are the same;
102    * <code>false</code> otherwise.
103    */

104   public boolean equals(Object JavaDoc obj) {
105     if (obj == null || !(obj instanceof SocketAddress))
106       return false;
107     SocketAddress sa = (SocketAddress) obj;
108
109     if ((hostname.equals(sa.hostname)) &&
110         (addr != null && addr.equals(sa.addr)) &&
111         (port == sa.port))
112       return true;
113     return false;
114   }
115
116   /**
117    * Constructs a string representation of this InetSocketAddress.
118    *
119    * @return a string representation of this object.
120    */

121   public String JavaDoc toString() {
122     StringBuffer JavaDoc strBuf = new StringBuffer JavaDoc();
123     strBuf.append("(").append(super.toString());
124     strBuf.append(",hostname=").append(hostname);
125     strBuf.append(",port=").append(port);
126     strBuf.append(",addr=").append(addr);
127     strBuf.append(")");
128     return strBuf.toString();
129   }
130 }
131
Popular Tags