HPsocketCS, TCP communication connection and sending

Use HPsocketCS to connect and send TCP.
The key point is when the connection receives data and sends data.
Step 1: Reference HPsocket to the project file
insert image description here
Step 2: Write the reference to the header file

using HPSocketCS;

Step 3: Create a TCP object

TcpServer hpServer = new TcpServer();

Step Four: Create Connections and Connection Events


            hpServer.OnAccept += HpServer_OnAccept1; //连接事件
            hpServer.OnClose += HpServer_OnClose1;   //断开连接事件
            // 接收数据
            hpServer.OnReceive += HpServer_OnReceive1;
            // 发送数据
            hpServer.OnSend += HpServer_OnSend;
            // 设置服务端IP
            //这里我直接将IP地址写死了,IP地址是一串string类型的字符串
            //所以我们可以在Winform中通过TextBox的形式将IP写入其中
            hpServer.IpAddress = "127.0.0.1";
            // 设置端口
            //端口号是一个Int类型的数字
            hpServer.Port = Convert.ToUInt16(8000);
            hpServer.SendPolicy = SendPolicy.Direct;
            //在这里我将上位机作为服务端,PLC作为客户端
            //服务端开启
            hpServer.Start();
            string strMsg = "服务端已开启并监听.";
            listBoxMsg.Items.Add(strMsg);
            listBoxMsg.TopIndex = listBoxMsg.Items.Count - 1;
            strMsg = "服务端监听Socket为{0}:{1}.";
            listBoxMsg.Items.Add(strMsg);
            listBoxMsg.TopIndex = listBoxMsg.Items.Count - 1;
            string ipListen = "";
            ushort portListen = 0;
            hpServer.GetListenAddress(ref ipListen, ref portListen);

The fifth step is to create the corresponding connection event.
Since the sending signal of TCP is in a fast loop in the TCP thread, we cannot write the sending signal in the OnSend function when sending the signal.
So we write the acceptance signal in the receiving data, then call the program we want to run, and send the signal after the running program ends

 private HandleResult HpServer_OnSend(IntPtr connId, byte[] bytes)
        {
    
    
            return HandleResult.Ok;
        }
        /// <summary>
        /// TCP接受数据,connIdTCP连接句柄,dataTCP获取到的数据
        /// </summary>
        /// <param name="connId"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        private HandleResult HpServer_OnReceive1(IntPtr connId, byte[] data)
        {
    
    
            string test;
            try
            {
    
    
                test = Encoding.Default.GetString(data);
                string strMsg_01 = "服务器接受数据: " + test;
                listBoxMsg.Items.Add(strMsg_01);
                listBoxMsg.TopIndex = listBoxMsg.Items.Count - 1;
                if (test == "abcde")
                {
    
    
                //user()为工作运行程序
                    User();
                    test = "";
                    //在运行程序结束后发送数据出去
                    TCPSend()}
                return HandleResult.Ok;
            }
            catch (Exception)
            {
    
    
                return HandleResult.Ignore;
            }
        }
        private void TCPSend()
        {
    
    
            string sendcenter = Convert.ToString(centerx * 1000) + Convert.ToString(centery * 1000);
            byte[] sendByetcenter = Encoding.Default.GetBytes(sendcenter);
            hpServer.Send(ID, sendByetcenter, sendByetcenter.Length);
        }
        private HandleResult HpServer_OnClose1(IntPtr connId, SocketOperation enOperation, int errorCode)
        {
    
    
            return HandleResult.Ok;
        }

        private HandleResult HpServer_OnAccept1(IntPtr connId, IntPtr pClient)
        {
    
    
            //tCP运行句柄
            ID = connId;
            return HandleResult.Ok;
        }
         private void TCPSend()
        {
    
    
        //sendcenter这个是我们需要发送的信息,先将其转化为字符串再转化为byte类型
            string sendcenter = Convert.ToString(centerx*1000) + Convert.ToString(centery * 1000);
            byte[] sendByetcenter = Encoding.Default.GetBytes(sendcenter);
            //ID是TCP的句柄,我们可以再连接事件中将ID进行赋值,ID的类型是IntPtr的句柄类型
            hpServer.Send(ID, sendByetcenter, sendByetcenter.Length);
        }

Renderings:
insert image description here

Guess you like

Origin blog.csdn.net/m0_51559565/article/details/128230637