java Socket 客户端实践

来源:互联网 发布:spring ioc 源码分析 编辑:程序博客网 时间:2024/05/18 14:25
 

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class SocketClient {
 protected final static Log log = LogFactory.getLog(SocketClient.class);
 
 private static final int RECEIVE_BUFFER_SIZE = 102400;
 
 private Socket clientSocket = null;
 
 private String host = "127.0.0.1";
 
 private int port = 6600;
 
 private BufferedInputStream in;

    private BufferedOutputStream out;
   
    @SuppressWarnings("unused")
 private String charset = "gbk";
   
    public SocketClient(String host, int port) {
     this.host = host;
     this.port = port;
    }
   
    /**
  *
  * 发送信息
  *
  * @param bData
  * name 线程名
  */
 public void send(byte[] bData, String name) {
        try {
         log.debug("线程:" + name + "    send data..... ");
            out.write(bData);
            out.flush();
        } catch (Throwable ex) {
            throw new RuntimeException(ex);
        }
    }
 
 /**
  * 建立SOCKET连接
  * name 线程名
  * @throws Throwable
  * @throws IOException
  * @throws UnknownHostException
  * @throws IOException
  * @throws UnknownHostException
  */
 public void openSocket(String name) {
  try {
  log.debug("线程:" + name + "   主机:" + host + ";端口:" + port + " 建立连接");
  clientSocket = new Socket(host, port);
  in = new BufferedInputStream(clientSocket.getInputStream());
     out = new BufferedOutputStream(clientSocket.getOutputStream());
  } catch (Throwable e) {
   throw new RuntimeException("线程:" + name + "   主机:" + host + ";端口:" + port + "  异常。 ", e);
  }
 }
 
 /**
  *
  * 发送信息
  *
  * @param bData
  *
  */
 public void send(byte[] bData) {
        try {
            out.write(bData);
            out.flush();
        } catch (Throwable ex) {
            throw new RuntimeException(ex);
        }
    }
 
 /**
  * 建立SOCKET连接
  * name 线程名
  */
 public void openSocket() {
  try {
   log.debug("   主机:" + host + ";端口:" + port + " 建立连接");
   clientSocket = new Socket(host, port);
   in = new BufferedInputStream(clientSocket.getInputStream());
         out = new BufferedOutputStream(clientSocket.getOutputStream());
  } catch (Throwable e) {
   throw new RuntimeException(e);
  }
 }
 
 /**
  * 断开SOCKET连接
  */
 public void closeSocket() {
  if (clientSocket != null) {
   try {
    log.debug("close socket");
    clientSocket.close();
   } catch (Throwable e) {
    throw new RuntimeException(e);
   }
  }
  clientSocket = null;
    }
 
 /**
  * name 线程名称
  *
  * 断开SOCKET连接
  */
 public void closeSocket(String name) {
  if (clientSocket != null) {
   try {
    log.debug("线程:" + name + "close socket");
    clientSocket.close();
   } catch (Throwable e) {
    throw new RuntimeException(e);
   }
  }
  clientSocket = null;
    }
 
 /**
  * 读取回传信息
  *
  * @return
  * @throws Exception
  * @throws SocketException
  */
 public byte[] receive() throws Exception, SocketException {
        byte rsp[] = new byte[RECEIVE_BUFFER_SIZE];
        int iIndex = 0;
        if ((clientSocket == null) || (in == null)) {
         log.debug("套接口无效,无法读取数据");
         log.error("套接口无效,无法读取数据");
            throw new SocketException("套接口无效,无法读取数据");
        }
        int iTimeoutTimes = 600;
        int iAvailable = 0;
        while (true) {
            iAvailable = in.available();

            if (iAvailable != 0) {
                for (int i = 0; i < iAvailable; i++) {
                    int temp_data = in.read();
                    if (temp_data != -1) {
                        rsp[iIndex + 0] = (byte) (temp_data & 0x0ff);
                        iIndex++;
                        if (iIndex >= RECEIVE_BUFFER_SIZE) {
                         log.debug("缓冲区长度过小,无法继续接收CM返回结果!");
                         log.error("缓冲区长度过小,无法继续接收CM返回结果!");
                            throw new RuntimeException("缓冲区长度过小,无法继续接收CM返回结果!");
                        }
                    }
                }
                break;
            }
            Thread.sleep(200);
            iTimeoutTimes--;

            if (clientSocket == null)
                break;
        }
        byte[] result = new byte[iIndex];
  for (int i = 0; i < iIndex; i++) {
   result[i] = rsp[i];
  }
  return result;
 }
}

原创粉丝点击