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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80 public class TCPSocket implements ContextSocketFace {
81 static final String version_id = "@(#)$Id: TCPSocket.java,v 1.8 2009/03/05 15:56:19 birgita Exp $ Copyright Westhawk Ltd";
82
83 private ServerSocket serverSoc = null;
84 private Socket clientSoc = null;
85 private InetAddress sendToHostAddr;
86 private int sendToHostPort;
87 private InetAddress receiveFromHostAddr;
88 private int receiveFromHostPort;
89 private InetAddress locBindAddr;
90
91 public TCPSocket() {
92 }
93
94 public void create(int port, String bindAddr) throws IOException {
95 sendToHostPort = port;
96 receiveFromHostPort = sendToHostPort;
97 try {
98 locBindAddr = null;
99 if (bindAddr != null) {
100 locBindAddr = InetAddress.getByName(bindAddr);
101 }
102 serverSoc = new ServerSocket(sendToHostPort, 50, locBindAddr);
103 } catch (SocketException exc) {
104 String str = "Socket problem: port=" + port + ", bindAddr="
105 + bindAddr + " " + exc.getMessage();
106 throw (new IOException(str));
107 }
108 }
109
110 public void create(String host, int port, String bindAddr) throws IOException {
111 sendToHostPort = port;
112 receiveFromHostPort = sendToHostPort;
113 try {
114 sendToHostAddr = InetAddress.getByName(host);
115 receiveFromHostAddr = sendToHostAddr;
116 locBindAddr = null;
117 if (bindAddr != null) {
118 locBindAddr = InetAddress.getByName(bindAddr);
119 }
120 clientSoc = new Socket(sendToHostAddr, sendToHostPort, locBindAddr, 0);
121 } catch (SocketException exc) {
122 String str = "Socket problem: host=" + host + ", port=" + port
123 + ", bindAddr=" + bindAddr + " " + exc.getMessage();
124 throw (new IOException(str));
125 } catch (UnknownHostException exc) {
126 String str = "Cannot find host " + host + " " + exc.getMessage();
127 throw (new IOException(str));
128 }
129 }
130
131 public String getReceivedFromHostAddress() {
132 String res = null;
133 if (receiveFromHostAddr != null) {
134 res = receiveFromHostAddr.getHostAddress();
135 }
136 return res;
137 }
138
139 public String getSendToHostAddress() {
140 String res = null;
141 if (sendToHostAddr != null) {
142 res = sendToHostAddr.getHostAddress();
143 }
144 return res;
145 }
146
147 public String getLocalSocketAddress() {
148 String res = null;
149 if (serverSoc != null) {
150 SocketAddress sa = serverSoc.getLocalSocketAddress();
151 if (sa != null) {
152 res = sa.toString();
153 }
154 } else if (clientSoc != null) {
155 SocketAddress sa = clientSoc.getLocalSocketAddress();
156 if (sa != null) {
157 res = sa.toString();
158 }
159 }
160 return res;
161 }
162
163 public String getRemoteSocketAddress() {
164 String res = null;
165 if (clientSoc != null) {
166 SocketAddress sa = clientSoc.getRemoteSocketAddress();
167 if (sa != null) {
168 res = sa.toString();
169 }
170 } else if (serverSoc != null) {
171
172 }
173 return res;
174 }
175
176 public StreamPortItem receive(int maxRecvSize) throws IOException {
177 StreamPortItem item = null;
178 if (serverSoc != null) {
179 byte[] data = new byte[maxRecvSize];
180
181
182
183
184 serverSoc.setSoTimeout(1000);
185
186 Socket newSocket = serverSoc.accept();
187
188
189 InputStream newSocketIn = newSocket.getInputStream();
190 newSocketIn.read(data, 0, data.length);
191
192 receiveFromHostAddr = newSocket.getInetAddress();
193 receiveFromHostPort = newSocket.getPort();
194
195 ByteArrayInputStream in = null;
196 in = new ByteArrayInputStream(data, 0, data.length);
197 item = new StreamPortItem(receiveFromHostAddr.getHostAddress(),
198 receiveFromHostPort, in);
199
200 newSocketIn.close();
201 newSocket.close();
202
203 newSocketIn = null;
204 newSocket = null;
205 } else if (clientSoc != null) {
206 byte[] data = new byte[maxRecvSize];
207
208
209
210
211 clientSoc.setSoTimeout(1000);
212
213 InputStream cin = clientSoc.getInputStream();
214 cin.read(data, 0, data.length);
215
216 receiveFromHostAddr = clientSoc.getInetAddress();
217 receiveFromHostPort = clientSoc.getPort();
218
219 ByteArrayInputStream in = null;
220 in = new ByteArrayInputStream(data, 0, data.length);
221 item = new StreamPortItem(receiveFromHostAddr.getHostAddress(),
222 receiveFromHostPort, in);
223 }
224 return item;
225 }
226
227 public void send(byte[] packet) throws IOException {
228 if (clientSoc != null) {
229 OutputStream out = clientSoc.getOutputStream();
230 out.write(packet);
231 out.flush();
232 }
233 }
234
235 public void close() {
236 try {
237 if (clientSoc != null) {
238 clientSoc.close();
239 }
240 } catch (IOException exc) {
241 }
242
243 try {
244 if (serverSoc != null) {
245 serverSoc.close();
246 }
247 } catch (IOException exc) {
248 }
249
250 serverSoc = null;
251 clientSoc = null;
252 }
253
254 }