1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  package uk.co.westhawk.snmp.net;
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  import java.io.*;
53  import java.net.*;
54  import uk.co.westhawk.snmp.stack.*;
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  public class StandardSocket implements ContextSocketFace {
65      static final String version_id = "@(#)$Id: StandardSocket.java,v 1.13 2009/03/05 15:56:19 birgita Exp $ Copyright Westhawk Ltd";
66  
67      private DatagramSocket soc = null;
68      private InetAddress sendToHostAddr;
69      private int sendToHostPort;
70      private InetAddress receiveFromHostAddr;
71      private int receiveFromHostPort;
72      private InetAddress locBindAddr = null;
73  
74      public StandardSocket() {
75      }
76  
77      public void create(int port, String bindAddr) throws IOException {
78          sendToHostPort = port;
79          receiveFromHostPort = sendToHostPort; 
80          try {
81              locBindAddr = null;
82              if (bindAddr != null) {
83                  locBindAddr = InetAddress.getByName(bindAddr);
84              }
85              soc = new DatagramSocket(sendToHostPort, locBindAddr);
86          } catch (SocketException exc) {
87              String str = "Socket problem: port=" + port + ", bindAddr="
88                      + bindAddr + " " + exc.getMessage();
89              throw (new IOException(str));
90          }
91      }
92  
93      public void create(String host, int port, String bindAddr) throws IOException {
94          sendToHostPort = port;
95          receiveFromHostPort = sendToHostPort; 
96          try {
97              sendToHostAddr = InetAddress.getByName(host);
98              receiveFromHostAddr = sendToHostAddr; 
99              locBindAddr = null;
100             if (bindAddr != null) {
101                 locBindAddr = InetAddress.getByName(bindAddr);
102             }
103             InetSocketAddress isa = new InetSocketAddress(locBindAddr, 0);
104             soc = new DatagramSocket(isa);
105         } catch (SocketException exc) {
106             String str = "Socket problem: host=" + host + ", port=" + port
107                     + ", bindAddr=" + bindAddr + " " + exc.getMessage();
108             throw (new IOException(str));
109         } catch (UnknownHostException exc) {
110             String str = "Cannot find host " + host + " " + exc.getMessage();
111             throw (new IOException(str));
112         }
113     }
114 
115     public String getReceivedFromHostAddress() {
116         String res = null;
117         if (receiveFromHostAddr != null) {
118             res = receiveFromHostAddr.getHostAddress();
119         }
120         return res;
121     }
122 
123     public String getSendToHostAddress() {
124         String res = null;
125         if (sendToHostAddr != null) {
126             res = sendToHostAddr.getHostAddress();
127         }
128         return res;
129     }
130 
131     public String getLocalSocketAddress() {
132         String res = null;
133         if (soc != null) {
134             SocketAddress sa = soc.getLocalSocketAddress();
135             if (sa != null) {
136                 res = sa.toString();
137             }
138         }
139         return res;
140     }
141 
142     public String getRemoteSocketAddress() {
143         String res = null;
144         if (soc != null) {
145             SocketAddress sa = soc.getRemoteSocketAddress();
146             if (sa != null) {
147                 res = sa.toString();
148             }
149         }
150         return res;
151     }
152 
153     public StreamPortItem receive(int maxRecvSize) throws IOException {
154         StreamPortItem item = null;
155         if (soc != null) {
156             byte[] data = new byte[maxRecvSize];
157             DatagramPacket p = new DatagramPacket(data, maxRecvSize);
158 
159             
160             
161             
162             soc.setSoTimeout(1000);
163 
164             soc.receive(p);
165             receiveFromHostAddr = p.getAddress();
166             receiveFromHostPort = p.getPort();
167 
168             ByteArrayInputStream in = null;
169             in = new ByteArrayInputStream(p.getData(), 0, p.getLength());
170             item = new StreamPortItem(receiveFromHostAddr.getHostAddress(),
171                     receiveFromHostPort, in);
172             p = null;
173         }
174         return item;
175     }
176 
177     public void send(byte[] packet) throws IOException {
178         if (soc != null) {
179             DatagramPacket pack = new DatagramPacket(packet, packet.length,
180                     sendToHostAddr, sendToHostPort);
181             soc.send(pack);
182         }
183     }
184 
185     public void close() {
186         if (soc != null) {
187             soc.close();
188             soc = null;
189         }
190     }
191 
192 }