socket通信

1.socket

1.1 client

1.1.1 方式1

public static String signAndVerify(String message, String signServeHost, int signServePort)  {
        LOGGER.info("签名验签请求:" + message);
        Socket socket = null;
        OutputStream outs = null;
        BufferedOutputStream bouts = null;
        InputStream ins = null;
        InputStreamReader isr = null;
        BufferedReader br = null;
        try {
            socket = new Socket();
            // 设置连接请求超时时间10s
            socket.connect(new InetSocketAddress(signServeHost, signServePort), MAX_TIMEOUT);
            // 设置读操作超时时间15s,setSoTimeout()设置的是读取数据时阻塞链路的超时时间,原文(Enable/disable SO_TIMEOUT with the specified timeout, in milliseconds. With this option set to a non-zero timeout, a read() call on the InputStream associated with this Socket will block for only this amount of time. If the timeout expires, a java.net.SocketTimeoutException is raised, though the Socket is still valid. The option must be enabled prior to entering the blocking operation to have effect. The timeout must be > 0. A timeout of zero is interpreted as an infinite timeout.)
            socket.setSoTimeout(MAX_TIMEOUT);
            // 获取输出流
            outs = socket.getOutputStream();
            /* 写入请求数据 */
            bouts = new BufferedOutputStream(outs);
            bouts.write(message.getBytes("GBK"));
            bouts.flush();
            // 结束发送
            socket.shutdownOutput();
            // 获取输入流
            ins = socket.getInputStream();
            isr = new InputStreamReader(ins, "GBK");
            br = new BufferedReader(isr);
            /* 读取返回数据 */
            StringBuffer sb = new StringBuffer();
            String info = null;
            while ((info = br.readLine()) != null) {
                sb.append(info);
            }
            String resultMsg = sb.toString();
            LOGGER.info("返回:" + resultMsg);
            return resultMsg;
        } catch (Exception e) {
            LOGGER.error("失败", e);
            BankAdapterException te = new BankAdapterException("失败" + e.getMessage(), e);
            te.setErrorCode(String.valueOf(ErrorCode.RECEIVE_FROM_BANK_ERROR));
            throw te;
        } finally {
            try {
                br.close();
                isr.close();
                ins.close();
                socket.close();
            } catch (IOException e) {
                LOGGER.error("释放异常");
            }
        }
    }

1.1.2 方式2

public String sendMessageRequest(String message, String bankFrontIP, int bankFrontPort) throws BankAdapterException {
        Socket socket = null;
        OutputStream outs = null;
        PrintWriter pw = null;
        InputStream ins = null;
        InputStreamReader isr = null;
        BufferedReader br = null;
        try {
            try {
                // 建立sockt连接
                socket = new Socket();
                // 设置连接请求超时时间15s
                socket.connect(new InetSocketAddress(bankFrontIP, bankFrontPort), 15000);
                // 设置读操作超时时间30s
                socket.setSoTimeout(30000);
                // 获取输出流
                outs = socket.getOutputStream();
                /* 写入请求数据 */
                pw = new PrintWriter(outs);
                pw.print(message);
                pw.flush();
                socket.shutdownOutput();
            } catch (Exception e) {
                LOGGER.error("报文发送异常", e);
                BankAdapterException te = new BankAdapterException("报文发送异常:" + e.getMessage(), e);
                te.setErrorCode(String.valueOf(ErrorCode.SEND_TO_BANK_ERROR));
                throw te;
            }
            try {
                // 获取输入流
                ins = socket.getInputStream();
                isr = new InputStreamReader(ins);
                br = new BufferedReader(isr);
                /* 读取返回数据 */
                StringBuffer sb = new StringBuffer();
                String info = null;
                while ((info = br.readLine()) != null) {
                    sb.append(info);
                }
 
                return sb.toString();
            } catch (Exception e) {
                LOGGER.error("接收回复异常", e);
                BankAdapterException te = new BankAdapterException("接收回复异常:" + e.getMessage(), e);
                te.setErrorCode(String.valueOf(ErrorCode.RECEIVE_FROM_BANK_ERROR));
                throw te;
            }
 
        }finally {
            try {
                socket.shutdownInput();
                ins.close();
                isr.close();
                br.close();
            } catch (IOException e) {
                LOGGER.error("关闭输入流失败");
            }
            try {
                LOGGER.info("正在关闭套接字");
                socket.close();
            } catch (IOException e) {
                LOGGER.error("关闭套接字失败");
            }
        }
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值