package com;



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

public class Client {
    // public static final int port = 8090;
    public static final int port = 8090;
    public static final String host = "14.1.209.146";
    // public static final String host = "127.0.0.1";
    public static void main(String[] args) {
        System.out.println("Client Start...");
        // String str = "00000604<?xml version=\"1.0\" encoding=\"gbk\"?><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>";
        // String str = "00000666<?xml version=\"1.0\" encoding=\"UTF-8\"?><Service><Service_Header><service_sn>1010157060000081243</service_sn><system_id>0258</system_id><requester_id>0170</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><oaTrvlBnsExpnsAcctNo>BTSQD-202309200005</oaTrvlBnsExpnsAcctNo><apprvrNo>10110567</apprvrNo><apprvrName>10110567</apprvrName><flowStatus>2</flowStatus><sgntrOpn>测试退回签字意见?</sgntrOpn></request></Service_Body></Service>";
        // String str = "<?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>";
        // String str = "00000669<?xml version=\"1.0\" encoding=\"UTF-8\"?><Service><Service_Header><service_sn>000000000004</service_sn><system_id>0258</system_id><requester_id>0170</requester_id><branch_id>951101</branch_id><channel_id>0170</channel_id><service_time>20230920195236</service_time><need_request>true</need_request><SvcCd>500120043</SvcCd><SvcScn>01</SvcScn><BnkSrlNo>017020230920000000000004</BnkSrlNo><FileFlg>0</FileFlg></Service_Header><Service_Body><request><oaTrvlBnsExpnsAcctNo>BTSQD-202309210011</oaTrvlBnsExpnsAcctNo><apprvrNo>10110001</apprvrNo><apprvrName>测试审批人</apprvrName><flowStatus>2</flowStatus><sgntrOpn>驳回测试2</sgntrOpn></request></Service_Body></Service>";
        // String str = "00000597<?xml version=\"1.0\" encoding=\"UTF-8\"?><Service><Service_Header><service_sn>1010157000000001757</service_sn><system_id>0258</system_id><requester_id>0157</requester_id><branch_id>010231100</branch_id><channel_id>01</channel_id><service_time>20231018135840</service_time><need_request>true</need_request><SvcCd>500130004</SvcCd><SvcScn>13</SvcScn><BnkSrlNo>015720231018000000001757</BnkSrlNo><FileFlg>0</FileFlg></Service_Header><Service_Body><request><path>todo</path><method>getCount2</method><args><entry><key>userNum</key><value>10110567</value></entry></args></request></Service_Body></Service>";
        // String str = "00000596<?xml version=\"1.0\" encoding=\"UTF-8\"?><Service><Service_Header><service_sn>1010157000000001757</service_sn><system_id>0258</system_id><requester_id>0157</requester_id><branch_id>010231100</branch_id><channel_id>01</channel_id><service_time>20231018135840</service_time><need_request>true</need_request><SvcCd>500130004</SvcCd><SvcScn>13</SvcScn><BnkSrlNo>015720231018000000001757</BnkSrlNo><FileFlg>0</FileFlg></Service_Header><Service_Body><request><path>todo</path><method>getCount</method><args><entry><key>userNum</key><value>10110567</value></entry></args></request></Service_Body></Service>";
        String str = "00000664<?xml version=\"1.0\" encoding=\"UTF-8\"?><Service><Service_Header><service_sn>000000000003</service_sn><system_id>0258</system_id><requester_id>0170</requester_id><branch_id>951101</branch_id><channel_id>0170</channel_id><service_time>20240329082246</service_time><need_request>true</need_request><SvcCd>500120043</SvcCd><SvcScn>01</SvcScn><BnkSrlNo>017020240329000000000003</BnkSrlNo><FileFlg>0</FileFlg></Service_Header><Service_Body><request><oaTrvlBnsExpnsAcctNo>FHBTSQD-202403250001</oaTrvlBnsExpnsAcctNo><apprvrNo>11010036</apprvrNo><apprvrName>赵圣熙</apprvrName><flowStatus>0</flowStatus><sgntrOpn>驳回测试</sgntrOpn></request></Service_Body></Service>";        // ;
        // System.out.println(XMLUtils.parseXMLToMap(str));
        // Map<String, String> stringStringMap = XMLUtils.parseXMLToMap(str);
        // System.out.println(stringStringMap);
        System.out.println(str.getBytes(StandardCharsets.UTF_8).length);
        // System.exit(0);
//        while (true) {
            Socket socket = null;
            try {
                //创建一个流套接字并将其连接到指定主机上的指定端口号  
                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(str);
                //读取服务器端数据
                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(str);

                String ret = input.readLine();
                System.out.println("服务器端返回过来的是: " + ret);
                // 如接收到 "OK" 则断开连接    
                if ("OK".equals(ret)) {
                    System.out.println("客户端将关闭连接");
                    Thread.sleep(500);
//                    break;
                }

                out.close();
                input.close();
            } catch (Exception e) {
                System.out.println("客户端异常:" + e.getMessage());
            } finally {
                if (socket != null) {
                    try {
                        socket.close();
                    } catch (IOException e) {
                        socket = null;
                        System.out.println("客户端 finally 异常:" + e.getMessage());
                    }
                }
            }
//        }
    }
}
