CCD C# Socket 通讯

一般情况下CCD会和PLC 单机 机器人通讯的比较多,普遍用TCP协议通讯,CCD这里可以用Socket写一个服务端和他们基于TCP协议通讯

下面是CCD作为服务端C#编写的代码:直接调用InitTCP()方法 开启Socket服务端

        private static byte[] result = new byte[1024];
        private static int myProt = 4996;   //端口号 
        private static Socket serverSocket;
        private static Socket socketSend;
        //创建监听连接的线程
        Thread AcceptSocketThread;
        Thread threadReceive;
        private void InitTCP()
        {

            if (this.InvokeRequired)
            {
                this.Invoke(new MethodInvoker(delegate
                {
                    InitTCP();
                }));
            }
            else
            {
                IPAddress ip = IPAddress.Parse(GetIpAddress());
                serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                serverSocket.Bind(new IPEndPoint(ip, myProt));  //绑定IP地址:端口  
                serverSocket.Listen(10);    //设定最多10个排队连接请求  
                Console.WriteLine("启动监听{0}成功", serverSocket.LocalEndPoint.ToString());
                //通过Clientsoket发送数据  
                //Thread myThread = new Thread(ListenClientConnect);
                //myThread.Start();
                AcceptSocketThread = new Thread(new ParameterizedThreadStart(ListenClientConnect));
                AcceptSocketThread.IsBackground = true;
                AcceptSocketThread.Start(serverSocket);
            }

        }
        /// 
        /// 获取本地IP
        /// 
        /// 
        private string GetIpAddress()
        {
            string hostName = Dns.GetHostName();   //获取本机名
            IPHostEntry localhost = Dns.GetHostByName(hostName);    //方法已过期,可以获取IPv4的地址
            //IPHostEntry localhost = Dns.GetHostEntry(hostName);   //获取IPv6地址
            IPAddress localaddr = localhost.AddressList[0];
            return localaddr.ToString();
        }
        ///   
        /// 接收消息  
        ///   
        ///   
        private void ReceiveMessage(object obj)
        {
            var exceptionStep = string.Empty;
            Socket socketSend = obj as Socket;
            while (true)
            {
                try
                {
                    exceptionStep = "ER1001";
                    //通过clientSocket接收数据  
                    int receiveNumber = ((Socket)socketSend).Receive(result);
                    if (receiveNumber == 0)//count 表示客户端关闭,要退出循环
                    {
                        //断开连接
                        break;
                    }
                    else
                    {
                        exceptionStep = "ER1003";
                        var receiveMsg = Encoding.ASCII.GetString(result, 0, receiveNumber);
                        if (receiveMsg.Trim() == "接受字符串")
                        {
                            var sendMsg="";
                            socketSend.Send(Encoding.ASCII.GetBytes(sendMsg));
                        }
                    }
                }
                catch (Exception ex)
                {
                    try
                    {
                        if (socketSend != null)
                        {
                            socketSend.Shutdown(SocketShutdown.Both);
                            socketSend.Close();
                        }
                    }
                    catch (Exception ex2)
                    {

                    }
                    break;
                }
            }

        }

        ///   
        /// 监听客户端连接  
        ///   
        private void ListenClientConnect(object obj)
        {
            var exceptionStep = string.Empty;
            while (true)
            {
                try
                {
                    Socket clientSocket = serverSocket.Accept();
                    SetTCPConnecInfo(clientSocket.RemoteEndPoint + " 连接成功");
                    socketSend = clientSocket;
                    clientSocket.Send(Encoding.ASCII.GetBytes("OK"));
                    threadReceive = new Thread(new ParameterizedThreadStart(ReceiveMessage));
                    threadReceive.IsBackground = true;
                    threadReceive.Start(socketSend);
                }
                catch (Exception ex)
                {

                }
            }

        }

 

你可能感兴趣的:(视觉CCD通讯,Socket通讯)