KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > communication > sms > util > ClientSocket


1 /***************************************************************************
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved. *
3  * Please look at license.txt in info directory for more license detail. *
4  **************************************************************************/

5 package org.exoplatform.services.communication.sms.util;
6
7 import java.io.BufferedReader JavaDoc;
8 import java.io.IOException JavaDoc;
9 import java.io.InputStream JavaDoc;
10 import java.io.InputStreamReader JavaDoc;
11 import java.io.OutputStream JavaDoc;
12 import java.io.PrintWriter JavaDoc;
13 import java.net.InetSocketAddress JavaDoc;
14 import java.net.Socket JavaDoc;
15 import java.net.SocketAddress JavaDoc;
16 import java.net.UnknownHostException JavaDoc;
17 import java.nio.ByteBuffer JavaDoc;
18 import java.nio.CharBuffer JavaDoc;
19 import java.nio.charset.CharacterCodingException JavaDoc;
20 import java.nio.charset.Charset JavaDoc;
21 import java.nio.charset.CharsetDecoder JavaDoc;
22 import java.nio.charset.CharsetEncoder JavaDoc;
23 import org.exoplatform.services.communication.sms.common.CommunicationError;
24
25
26 /**
27  * @author: Ove Ranheim
28  * @email: oranheim@users.sourceforge.net
29  */

30 public class ClientSocket {
31
32     private static final int SOCKET_TIMEOUT_IN_MILLIS = 10 * 1000;
33     private static final int SOCKET_BUFFER_SIZE = 8 * 1024;
34     private String JavaDoc _ipAddress = "";
35     private int _port = 0;
36     private Charset JavaDoc _charset;
37     private CharsetEncoder JavaDoc _charSetEncoder;
38     private CharsetDecoder JavaDoc _charSetDecoder;
39
40     public ClientSocket(String JavaDoc host, int port) {
41         _ipAddress = host;
42         _port = port;
43
44         _charset = Charset.forName("ISO-8859-1");
45         _charSetEncoder = _charset.newEncoder();
46         _charSetDecoder = _charset.newDecoder();
47     }
48
49     private byte[] charArrayToByteArray(char[] chars, int offset, int len) throws CharacterCodingException JavaDoc {
50         CharBuffer JavaDoc charBuf = CharBuffer.wrap(chars, offset, len);
51         return _charSetEncoder.encode(charBuf).array();
52     }
53
54     private char[] byteArrayToCharArray(byte[] bytes, int offset, int len) throws CharacterCodingException JavaDoc {
55         ByteBuffer JavaDoc byteBuf = ByteBuffer.wrap(bytes, offset, len);
56         return _charSetDecoder.decode(byteBuf).array();
57     }
58
59     public String JavaDoc sendMessage(String JavaDoc buffer) throws CommunicationError {
60         StringBuffer JavaDoc inputBuffer = new StringBuffer JavaDoc();
61         PrintWriter JavaDoc out = null;
62         BufferedReader JavaDoc in = null;
63         Socket JavaDoc writeSocket = new Socket JavaDoc();
64         try {
65             try {
66                 SocketAddress JavaDoc remote = new InetSocketAddress JavaDoc(_ipAddress, _port);
67                 writeSocket.connect(remote, SOCKET_TIMEOUT_IN_MILLIS);
68                 OutputStream JavaDoc os = writeSocket.getOutputStream();
69                 InputStream JavaDoc is = writeSocket.getInputStream();
70
71                 out = new PrintWriter JavaDoc(os, true);
72                 try {
73                     in = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(is));
74                     try {
75                         // Send payload
76
byte[] b_out = buffer.getBytes();
77                         int p = 0;
78                         int n = b_out.length;
79                         while (n > 0) {
80                             int m = SOCKET_BUFFER_SIZE;
81                             if (n < SOCKET_BUFFER_SIZE) m = n;
82                             char[] a = byteArrayToCharArray(b_out, p, m);
83                             out.write(a);
84                             p = p + m; // inc next read position
85
n = n - m; // dec remaning buffer
86
}
87                         out.println();
88
89                         // Read resposne
90
char[] b_in = new char[SOCKET_BUFFER_SIZE];
91                         while ((n = in.read(b_in)) > 0) {
92                             inputBuffer.append(b_in, 0, n);
93                         }
94                     } finally {
95                         in.close();
96                     }
97                 } finally {
98                     out.close();
99                 }
100             } catch (UnknownHostException JavaDoc e) {
101                 throw new CommunicationError("Unknown host: " + _ipAddress);
102             } finally {
103                 writeSocket.close();
104             }
105         } catch (IOException JavaDoc ioe) {
106             throw new CommunicationError(ioe);
107         }
108         return inputBuffer.toString();
109     }
110 }
111
112
Popular Tags