VisionPro二开之网口通讯设计
CommunicateService using System ; using System. Collections. Generic ; using System. Linq ; using System. Text ; using System. Threading. Tasks ; using System. Windows. Forms ; namespace AOI外观检测软件. Communicate{ /// <summary> /// 通讯服务类 /// </summary> public class CommunicateService { // 单例模式:使类成为全局唯一的存在 //优点1:方便使用 //优点2:节省资源 //单例模式: //1. 私有静态变量(在第四步供外界使用),创建类的实例 //2. 私有构造函数,确保外部无法直接实例化(确保是单个实例) //3. 确定供外界调用的代码资源 //4. 公开静态属性,供外界使用(把第一步类的实例,开放出去) //5. 外界使用 //1. 私有静态变量(在第四步供外界使用),创建类的实例 // static 静态: 类 方法 等 (1)在程序启动之前就生成,(2)在程序结束之后,才会消失,(3)全局唯一的存在。 private static CommunicateService intance= new CommunicateService ( ) ; //2. 私有构造函数,确保外部无法直接实例化(确保是单个实例) // private 修饰 就变成私有的 ,不允许外界调用,确保单例模式,唯一的特性 private CommunicateService ( ) { } //4. 公开静态属性,供外界使用(把第一步类的实例,开放出去) // public static 构建了开发 public static CommunicateService Instance{ get { return intance; } } //3. 确定供外界调用的代码资源 HttpServer server= new HttpServer ( ) ; /// <summary> /// 启动服务端 /// </summary> /// <returns></returns> public bool Start ( string ip, int port) { server. IP= ip; server. Port= port; if ( server. Start ( ) ) { return true ; } else { return false ; } } /// <summary> /// 发送数据 /// </summary> /// <param name="content"></param> public void Send ( string content) { server. SendMsg ( content) ; } } } HttpServer using System ; using System. Collections. Generic ; using System. Linq ; using System. Net ; using System. Net. Sockets ; using System. Text ; using System. Threading. Tasks ; using System. Windows. Forms ; using AOI外观检测软件. Camera; namespace AOI外观检测软件. Communicate{ /// <summary> /// 服务端 /// </summary> public class HttpServer { // Http-Server(服务端): // 1:创建socket() // 2:设置IP和端口 // 3. 绑定ip和端口 // 4:listen()监听,确定能连接多少个客户端 // 5:accept()函数接受客户端的连接 // 6:接受数据 // 7: 发送数据 // 8:终止连接。 // 1:创建socket() public Socket SocketSever; // 创建接受客户端的字典(就是成对放的数组),发送数据给客户端的时候要用 public Dictionary< string , Socket> CurrentClientlist= new Dictionary< string , Socket> ( ) ; #region 属性 /// <summary> /// 服务IP /// </summary> public string IP{ get ; set ; } /// <summary> /// 端口 /// </summary> public int Port{ get ; set ; } #endregion /// <summary> /// 启动 /// </summary> public bool Start ( ) { try { // 1:创建socket() SocketSever= new Socket ( AddressFamily. InterNetwork, SocketType. Stream, ProtocolType. Tcp) ; // 2:设置IP和端口。 IPEndPoint ipe= new IPEndPoint ( IPAddress. Parse ( IP) , Port) ; try { // 3. 绑定ip和端口 SocketSever. Bind ( ipe) ; } catch ( Exception ex) { MessageBox. Show ( "服务器开启失败:" + ex. Message) ; return false ; } // 4:listen(),确定能连接多少个客户端: 10 允许最多10个连接在队列中等待 SocketSever. Listen ( 10 ) ; // 5.创建一个监听的线程 Task. Run ( new Action ( ( ) => { AcceptClients ( ) ; } ) ) ; return true ; } catch ( Exception exp) { MessageBox. Show ( "服务器开启失败:" + exp. Message) ; return false ; } } /// <summary> /// 监听客户端连接 /// </summary> public void AcceptClients ( ) { while ( true ) { // 5:accept()函数接受客户端的连接 Socket socketClient= SocketSever. Accept ( ) ; string client= socketClient. RemoteEndPoint. ToString ( ) ; // 将客户端保存起来 CurrentClientlist. Add ( client, socketClient) ; // 6:接受数据 Task. Run ( new Action ( ( ) => { ReceiveMessage ( socketClient) ; } ) ) ; } } /// <summary> /// 监听接收客户端数据 /// </summary> /// <param name="socketClient"></param> private void ReceiveMessage ( Socket socketClient) { while ( true ) { // 创建一个缓冲区 byte [ ] buffer= new byte [ 1024 * 1024 * 10 ] ; // 数据长度 int length= - 1 ; string client= socketClient. RemoteEndPoint. ToString ( ) ; try { length= socketClient. Receive ( buffer) ; } catch ( Exception ) { MessageBox. Show ( client+ "下线了" ) ; CurrentClientlist. Remove ( client) ; break ; } if ( length> 0 ) { string msg= string . Empty; // 以utf8的格式接受 msg= Encoding. UTF8. GetString ( buffer, 0 , length) ; //MessageBox.Show("接受信息:"+msg); // 触发拍照(在上位机或者PLC发送这个通讯信息的时候,我们进行解析以后,进行拍照) CameraService. Instance. SnapImage ( ) ; // 显示接受内容。需要使用Invoke,跨线程,跨UI //Invoke(new Action(() => //{ // rtb_Receive_server.AppendText(msg + "\n"); //})); } else { MessageBox. Show ( client+ "下线了" ) ; CurrentClientlist. Remove ( client) ; break ; } } } /// <summary> /// 发送数据utf8 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public void SendMsg ( string Content) { // 获取信息 byte [ ] sendMsg= Encoding. UTF8. GetBytes ( Content) ; // 对客户端发送信息 foreach ( var itemin this . CurrentClientlist) { // 发送数据 item. Value?. Send ( sendMsg) ; } } } }
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/1136395.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!