⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs

📁 8583包打包解包小应用
💻 CS
📖 第 1 页 / 共 2 页
字号:
using System;
using System.IO;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
//using My8583;

namespace My8583App
{
    public partial class Form1 : Form
    {
        
        private My8583 pk8583,unpk8583;
        private byte[] b_msg;
        private Thread th;
        IPAddress ServerIp;
        private TcpListener tcpl, listener;
        private TcpClient tcpc;
        private NetworkStream tcpStream;
        //private Socket clientsocket;
        private int listenport = 8700;
        //NetworkStream conStream;

        public Form1()
        {
            InitializeComponent();
            button6.Enabled = false;
            //init8583();
        }
        public void init8583()
        {
            pk8583 = new My8583();
            pk8583.InitTab();
            
        }

        public void pack_xf()
        {
            listBox1.Items.Add("f2:账号");
            listBox1.Items.Add("f4:金额");
            /***以下是打包***/
            pk8583.settabx_data(1, textBox1.Text);
            pk8583.settabx_len_act(1, textBox1.Text.Length);
            pk8583.settabx_data(2, "000000");
            pk8583.settabx_data(3, textBox2.Text);
            pk8583.settabx_data(10, "999999");
            //b_msg = pk8583.Pack8583("0200");

        }

        public static IPAddress GetServerIP()
        {


            IPHostEntry ieh = Dns.GetHostEntry(Dns.GetHostName());

            return ieh.AddressList[0];

        }

        void ct_MyEvent(string temp)
        {

            //设置服务器端TextBox的值
            this.richTextBox1.Text = temp;
        }



        /***采用接受单个客户端的短连接,多次请求****/
        private void Listen()
        {
            string readmsg = "";
            string echomsg = "";
            try
            {
                //IPAddress ServerIp = GetServerIP();
                //tcpl = new TcpListener(ServerIp, listenport);
                //tcpl.Start();
                //richTextBox1.Text += "正在监听" + string.Format("{0}:{1}\r\n", ServerIp.ToString(), listenport.ToString());

                TcpClient remoteClient = tcpl.AcceptTcpClient();
                NetworkStream streamToClient = remoteClient.GetStream();
                do
                {
                 
                       Byte[] buffer = new Byte[1024];
                        int bytesRead;
                        try
                        {
                            lock (streamToClient)
                            {
                                bytesRead = streamToClient.Read(buffer, 0, buffer.Length);
                            }
                            if (bytesRead == 0) throw new Exception("读取到0字节");
                                                                            
                            readmsg = string.Format("Client Send:{0}>>>{1}\r\n", bytesRead, System.BitConverter.ToString(buffer,0,bytesRead));
                            
                            Byte[] retbuffer = new Byte[1024];
                            echomsg = string.Format("服务端已接收到来自{0}的8583报文消息", remoteClient.Client.RemoteEndPoint);
                            buffer = System.Text.Encoding.Unicode.GetBytes(echomsg);
                             lock (streamToClient)
                            {
                                streamToClient.Write(buffer, 0, buffer.Length);
                             }

                         }
                        catch (Exception)
                        {
                            richTextBox1.AppendText("远程主机关闭了一个连接!");
                            break;
                        }
                    /***把接收和发送消息显示放在try之外,防止程序堵塞***/
                    richTextBox1.AppendText(readmsg);
                
                 } while (true);
                 streamToClient.Dispose();
                 remoteClient.Close();
                 richTextBox1.Text += "客户端断开连接"+"\r\n";
                 
            }
            catch (System.Security.SecurityException)
            {
                MessageBox.Show("防火墙安全错误!", "错误",
                    MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            catch (Exception)
            {
                richTextBox1.Text = "已停止监听!";
            }
        }

        private void Stop()
        {
            tcpl.Stop();
            th.Abort();
            
        }

        /***只负责发送,不需要连接***/
        private void Send()
        {
            int BufferSize = 8192;
            string msg = "hello,i am danny";
            byte[] buffer = Encoding.Unicode.GetBytes(msg);

            try
            {
                lock (tcpStream)
                {
                    tcpStream.Write(b_msg, 0, b_msg.Length);
                    //tcpStream.Write(buffer, 0, buffer.Length);
                }
          
                /***接受来自服务器的反馈信息***/
                
                Byte[] echobytes = new byte[BufferSize];
                int readlen;
                               
                lock (tcpStream)
                {  readlen = tcpStream.Read(echobytes, 0, BufferSize);
                }
                richTextBox2.Text += System.Text.Encoding.Unicode.GetString(echobytes,0,readlen)+"\r\n";
                           
            }
            catch (Exception)
            {
                richTextBox2.Text = "目标计算机拒绝发送消息请求!";
            }
           
        }

        /****短连接发送****/
        private void OneSend()
        {
            try
            {
                tcpc = new TcpClient(textBox3.Text, Int32.Parse(textBox4.Text));
                tcpStream = tcpc.GetStream();
                richTextBox2.Clear();
                /****开始发送***/
                try
                {
                    string temp = "hello ,i am danny";

                    StreamWriter sw = new StreamWriter(tcpStream);
                    StreamReader sr = new StreamReader(tcpStream);

                    //将TextBox1的值传给服务器端
                    sw.WriteLine(temp);
                    sw.Flush();

                    //接收服务器端回传的字符串
                    string str = sr.ReadLine();
                    //this.richTextBox2.Text = str;
                    sr.Close();
                    sw.Close();

                }
                catch (Exception)
                {
                    richTextBox2.Text = "目标计算机拒绝发送消息请求!";
                }
                tcpStream.Dispose();
                tcpc.Close();
                richTextBox2.Text = "发送完成,断开服务端连接!";

            }
            catch (Exception)
            {
                richTextBox2.Text = "目标计算机拒绝连接请求!";
            }

        }


        private void button1_Click(object sender, EventArgs e)
        {
            
            
            init8583();
            textBox1.Text = "4580651972506998";
            textBox2.Text="000000000188";
            pk8583.settabx_data(1, textBox1.Text);
            pk8583.settabx_len_act(1, textBox1.Text.Length);
            pk8583.settabx_data(3, textBox2.Text);
            pk8583.settabx_data(10, "123456");

            /***消费交易打包***/
            if (radioButton1.Checked == true)
               pack_xf();
         

           b_msg = pk8583.Pack8583("0200");
          
           //b_msg = pk8583.TestPack8583("0200");

            //for (int i = 0; i < b_msg.Length; i++)
            //richTextBox1.Text += b_msg[i].ToString("X2");
            //richTextBox1.AppendText("--"+System.Text.Encoding.Default.GetString(tbyte));

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -