package com.engine.util;

import weaver.general.BaseBean;
import weaver.general.StringUtil;

import java.io.*;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

//发送SocketUtil
public class SocketClientUtil {
    private int port = 8090;
    private String host = "14.1.209.146";
    // private BaseBean bs = new BaseBean();


    public SocketClientUtil(String host ,int port) {
        this.port = port;
        this.host = host;
    }

    public String send(String request) throws Exception {
        // bs.writeLog("Client Start...");
        Socket socket = null;
        try {
            //创建一个流套接字并将其连接到指定主机上的指定端口号
            // System.out.println(request);
            socket = new Socket(host,port);
            // String str = "00000612<?xml version=\"1.0\" encoding=\"UTF-8\"?><Service><Service_Header><service_sn>1010157060000081243</service_sn><system_id>0258</system_id><requester_id>0157</requester_id><branch_id>010231100</branch_id><channel_id>01</channel_id><service_time>20230905104426</service_time><need_request>true</need_request><SvcCd>500130004</SvcCd><SvcScn>13</SvcScn><BnkSrlNo>015720230905060000081243</BnkSrlNo><FileFlg>0</FileFlg></Service_Header><Service_Body><request><path>todo</path><method>getCount</method><args><entry><key>userNum</key><value>10913026任广鹏是个大刷币</value></entry></args></request></Service_Body></Service>";
            // System.out.println(request);
            //读取服务器端数据
            BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream(), StandardCharsets.UTF_8));
            //向服务器端发送数据
            // PrintStream out = new PrintStream(new OutputStreamWriter(socket.getOutputStream(),StandardCharsets.UTF_8));
            PrintWriter out =  new PrintWriter(new OutputStreamWriter(socket.getOutputStream(),StandardCharsets.UTF_8),true);
            out.println(request);
            char[] datalen=new char[8];//报文前八位为报文体总长度
            input.read(datalen,0,8);
            String lendata=new String (datalen);
            System.out.println(lendata);
            int length=Integer.parseInt(lendata);
            // new BaseBean().writeLog("报文长度"+length);
            char[] data=new char[length];
            int datalength = input.read(data,0,length);
            String requestData = new String(data);
            // new BaseBean().writeLog("requestData",requestData);
            // String ret = input.readLine();
            System.out.println("服务器端返回过来的是: " + requestData);
            // bs.writeLog("服务器端返回过来的是: " + requestData);
            // 如接收到 "OK" 则断开连接
            if (!StringUtil.isEmpty(requestData)) {
                // bs.writeLog("客户端将关闭连接");
                // System.out.println("客户端将关闭连接: " + requestData);
                Thread.sleep(500);
            }
            out.close();
            input.close();
            return requestData;
        } catch (Exception e) {
            System.out.println("客户端异常:" + e.getMessage());
            throw  e;
        } finally {
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    socket = null;
                    // bs.writeLog("客户端 finally 异常:" + e.getMessage());
                }
            }
        }
        // return "-1";
       }



       //测试
    public static void main(String[] args) throws Exception {
        for (int i = 0; i < 2000; i++) {
            new Thread(){
                @Override
                public void run(){
                    SocketClientUtil socket = new SocketClientUtil("14.1.209.146", 8090);
                    try {


                            socket.send("00000597<?xml version=\"1.0\" encoding=\"UTF-8\"?><Service><Service_Header><service_sn>1010157060000065445</service_sn><system_id>0258</system_id><requester_id>0157</requester_id><branch_id>010231100</branch_id><channel_id>01</channel_id><service_time>20240315092648</service_time><need_request>true</need_request><SvcCd>500130004</SvcCd><SvcScn>12</SvcScn><BnkSrlNo>015720240315060000065445</BnkSrlNo><FileFlg>0</FileFlg></Service_Header><Service_Body><request><path>todo</path><method>getCount2</method><args><entry><key>userNum</key><value>10110047</value></entry></args></request></Service_Body></Service>");

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }.start();
        }
    }
}
