- package cn.CodeSnippet.demo.io;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.net.Socket;
- import java.net.UnknownHostException;
- import com.neusoft.demo.utils.Constants;
- import com.neusoft.transfer.Converser;
- /**
- * <pre>
- * 采用JAVA IO的通信客户端
- * </pre>
- */
- public class ClientIo {
- private Socket socket;
- /**
- * 初始化连接
- * @throws UnknownHostException
- * @throws IOException
- */
- private void init() throws UnknownHostException, IOException {
- socket = new Socket(Constants.IO_SERVER_IP, Constants.IO_SERVER_PORT);
- }
- /**
- *
- */
- public void send() {
- try {
- InputStream br = getReader(socket);
- OutputStream pw = getWriter(socket);
- byte[] response = new byte[10];
- byte[] req = getRequest(0);
- pw.write(req);
- if (br.read(response) != -1) {
- System.out.println(echoMsg(response));
- }
- req = getRequest(2);
- pw.write(req);
- if (br.read(response) != -1) {
- System.out.println(echoMsg(response));
- }
- req = getRequest(4);
- pw.write(req);
- br.close();
- pw.close();
- socket.close();
- } catch (Throwable t) {
- t.printStackTrace();
- }
- }
- private String echoMsg(byte[] response) {
- StringBuilder sb = new StringBuilder();
- byte[] msgLen = new byte[4];
- System.arraycopy(response, 0, msgLen, 0, 4);
- byte[] commandId = new byte[4];
- System.arraycopy(response, 4, commandId, 0, 4);
- byte[] msgBody = new byte[2];
- System.arraycopy(response, 8, msgBody, 0, 2);
- sb.append("msgLen:" + Converser.bytes2Int(msgLen));
- sb.append(",commandId:" + Converser.bytes2Int(commandId));
- sb.append(",msgBody:" + new String(msgBody));
- return sb.toString();
- }
- /**
- * 生成request
- * @param commandId
- * @return
- */
- private byte[] getRequest(int commandId) {
- byte[] request = new byte[10];
- System.arraycopy(Converser.int2Byte(10), 0, request, 0, 4);
- switch (commandId) {
- case 0:
- System.arraycopy(Converser.int2Byte(commandId), 0, request, 4, 4);
- System.arraycopy("dl".getBytes(), 0, request, 8, 2);
- break;
- case 2:
- System.arraycopy(Converser.int2Byte(commandId), 0, request, 4, 4);
- System.arraycopy("yw".getBytes(), 0, request, 8, 2);
- break;
- case 4:
- System.arraycopy(Converser.int2Byte(commandId), 0, request, 4, 4);
- System.arraycopy("bb".getBytes(), 0, request, 8, 2);
- break;
- }
- return request;
- }
- /**
- * 获得输入流
- * @param socket
- * @return
- * @throws IOException
- */
- private InputStream getReader(Socket socket) throws IOException {
- InputStream inputStream = socket.getInputStream();
- return inputStream;
- }
- /**
- * 获取socket输出流
- * @param socket
- * @return
- * @throws IOException
- */
- private OutputStream getWriter(Socket socket) throws IOException {
- OutputStream outputStream = socket.getOutputStream();
- return outputStream;
- }
- /**
- * @param args
- */
- public static void main(String[] args) {
- ClientIo clientIo = new ClientIo();
- try {
- clientIo.init();
- clientIo.send();
- } catch (UnknownHostException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/061120136920.html
来源: http://www.codesnippet.cn/detail/061120136920.html