概念
HP-Socket 是国产的TCP、UDP和HTTP 通信框架,包含服务端、客户端和 Agent 组件,提供 C/C++、C#、Java、Python 等多种编程语言接口。
与传统TCP通讯区别的是HP-Socket 对通信层做到了完全封装,程序不必关注通信层的任何细节;HP-Socket提供基于事件通知模型的 API 接口,能简单高效地整合到新旧应用程序中。
示例
TcpServer hpServer;bool tcp1State = false;public Form1(){InitializeComponent();}enum PlcPort{Op1 = 4000,}private void Form1_Load(object sender, EventArgs e){hpServer = new TcpServer();hpServer.OnPrepareListen += OnPrepareListen;hpServer.OnAccept += OnAccept; //连接事件hpServer.OnClose += OnClose; //断开连接事件hpServer.OnReceive += OnReceive;// 接收数据hpServer.OnSend += OnSend;// 发送数据hpServer.Address = "127.0.0.1";// 设置服务端IPhpServer.Port = Convert.ToUInt16(PlcPort.Op1);// 设置端口hpServer.SendPolicy = SendPolicy.Direct;hpServer.Start();ReportMsg("服务端已开启并监听");}private HandleResult OnPrepareListen(IServer sender, IntPtr listen){return HandleResult.Ok;}private HandleResult OnAccept(IServer sender, IntPtr connId, IntPtr client){// 获取客户端地址if (!sender.GetRemoteAddress(connId, out var ip, out var port)){return HandleResult.Error;}return HandleResult.Ok;}//服务器收到数据private HandleResult OnReceive(IServer sender, IntPtr connId, byte[] data){try{ return HandleResult.Ok;}catch (Exception){return HandleResult.Ignore;}}private HandleResult OnSend(IServer sender, IntPtr connId, byte[] data){try{return HandleResult.Ok;}catch (Exception){return HandleResult.Ignore;}}//服务器关闭连接private HandleResult OnClose(IServer sender, IntPtr connId, SocketOperation socketOperation, int errorCode){return HandleResult.Ok;}//服务器关闭private HandleResult OnShutdown(IServer sender){return HandleResult.Ok;}