1 5 package org.exoplatform.services.communication.sms.util; 6 7 import java.io.BufferedReader ; 8 import java.io.IOException ; 9 import java.io.InputStream ; 10 import java.io.InputStreamReader ; 11 import java.io.OutputStream ; 12 import java.io.PrintWriter ; 13 import java.net.InetSocketAddress ; 14 import java.net.Socket ; 15 import java.net.SocketAddress ; 16 import java.net.UnknownHostException ; 17 import java.nio.ByteBuffer ; 18 import java.nio.CharBuffer ; 19 import java.nio.charset.CharacterCodingException ; 20 import java.nio.charset.Charset ; 21 import java.nio.charset.CharsetDecoder ; 22 import java.nio.charset.CharsetEncoder ; 23 import org.exoplatform.services.communication.sms.common.CommunicationError; 24 25 26 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 _ipAddress = ""; 35 private int _port = 0; 36 private Charset _charset; 37 private CharsetEncoder _charSetEncoder; 38 private CharsetDecoder _charSetDecoder; 39 40 public ClientSocket(String 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 { 50 CharBuffer 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 { 55 ByteBuffer byteBuf = ByteBuffer.wrap(bytes, offset, len); 56 return _charSetDecoder.decode(byteBuf).array(); 57 } 58 59 public String sendMessage(String buffer) throws CommunicationError { 60 StringBuffer inputBuffer = new StringBuffer (); 61 PrintWriter out = null; 62 BufferedReader in = null; 63 Socket writeSocket = new Socket (); 64 try { 65 try { 66 SocketAddress remote = new InetSocketAddress (_ipAddress, _port); 67 writeSocket.connect(remote, SOCKET_TIMEOUT_IN_MILLIS); 68 OutputStream os = writeSocket.getOutputStream(); 69 InputStream is = writeSocket.getInputStream(); 70 71 out = new PrintWriter (os, true); 72 try { 73 in = new BufferedReader (new InputStreamReader (is)); 74 try { 75 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; n = n - m; } 87 out.println(); 88 89 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 e) { 101 throw new CommunicationError("Unknown host: " + _ipAddress); 102 } finally { 103 writeSocket.close(); 104 } 105 } catch (IOException ioe) { 106 throw new CommunicationError(ioe); 107 } 108 return inputBuffer.toString(); 109 } 110 } 111 112 | Popular Tags |