示例#1
0
        /// <summary>
        /// 发送字节流
        /// </summary>
        /// <param name="server">targetIP</param>
        /// <param name="message">byte[] message</param>
        /// <returns></returns>
        public static ResultInfo SendMessage(string server, byte[] message)
        {
            bool   status        = false;
            int    resultCode    = 0;
            string Num           = string.Empty;
            string CMD           = string.Empty;
            string resultMessage = string.Empty;

            TcpClient     client = new TcpIpHelper(server).Connect();
            NetworkStream ns     = client.GetStream();

            try
            {
                Byte[] sendData = message;
                log.DebugFormat("发送消息到【{0}】,内容为:{1},\r\n*********len:{2}", server, DefaultEncoding.GetString(message), sendData.Length);
                ns.Write(sendData, 0, sendData.Length);//写入字节流

                //接收服务端的响应数据
                byte[] recieveData  = new Byte[1024];
                string responseData = string.Empty;
                ns.ReadTimeout = RECEIVE_TIMEOUT;//读取服务端返回信息超时时间(ms),建议配置在webconfig中 xyl 2013-03-07
                Int32 bytes = ns.Read(recieveData, 0, recieveData.Length);

                responseData = System.Text.Encoding.Default.GetString(recieveData, 0, bytes);
                log.DebugFormat("接收消息到【{0}】,内容为:{1}", server, responseData);
                //处理返回信息,返回数据为xml格式的字串
                //1、操作成功:status = true
                GetResultXML(responseData, out Num, out CMD, out status, out resultCode, out resultMessage);
                //status = true;
                //resultMessage = "操作成功";

                //2、操作失败:status = false
                //status = false;
                //resultMessage = "从返回结果中解析并说明失败原因";
            }

            catch (Exception e)
            {
                //异常处理
                SocketException se  = (SocketException)e.InnerException.GetBaseException();
                string          msg = string.Empty;
                switch (se.ErrorCode)
                {
                case 10060:
                    msg = string.Format(" 连接<font color=blue>控制系统 <b>{0}</b></font>处理超时",
                                        server);
                    break;

                case 10061:
                    msg = string.Format(" 连接<font color=blue>控制系统 <b>{0}</b></font> 超时,可能原因是机器或打料系统未启动",
                                        server);
                    break;

                case 10065:
                    msg = string.Format(" 无法连接<font color=blue>控制系统 <b>{0}</b></font>,可能网线没有插好,请检查网络连接!",
                                        server);
                    break;

                default:
                    msg = se.Message;
                    break;
                }
                log.Error(msg);
                throw new Exception(msg);
            }
            finally
            {
                //关闭流
                ns.Close();
                //关闭连接
                client.Close();
            }
            return(new ResultInfo
            {
                Result = status,
                ResultCode = resultCode,
                Message = "[控制系统:" + server + "]" + resultMessage
            });
        }
示例#2
0
        /// <summary>
        /// 发送字符串
        /// </summary>
        /// <param name="server"></param>
        /// <param name="message">message信息为xml形式的字串</param>
        public static dynamic SendMessage(string server, string message)
        {
            bool   status        = false;
            int    resultCode    = 0;
            string Num           = string.Empty;
            string CMD           = string.Empty;
            string resultMessage = string.Empty;

            try
            {
                TcpClient client = new TcpIpHelper(server).Connect();


                Byte[] sendData = DefaultEncoding.GetBytes(message);

                NetworkStream ns = client.GetStream();

                log.DebugFormat("发送消息到【{0}】,内容为:{1}", server, message);
                ns.Write(sendData, 0, sendData.Length);//写入字节流

                //接收服务端的响应数据
                byte[] recieveData  = new Byte[1024];
                string responseData = string.Empty;
                ns.ReadTimeout = client.ReceiveTimeout;
                Int32 bytes = ns.Read(recieveData, 0, recieveData.Length);
                responseData = System.Text.Encoding.Default.GetString(recieveData, 0, bytes);
                log.DebugFormat("接收消息到【{0}】,内容为:{1}", server, responseData);
                GetResultXML(responseData, out Num, out CMD, out status, out resultCode, out resultMessage);

                //处理返回信息,返回数据为xml格式的字串
                //1、操作成功:status = true
                //status = true;
                //resultMessage = "操作成功";

                //2、操作失败:status = false
                //status = false;
                //resultMessage = "从返回结果中解析并说明失败原因";

                //关闭流
                ns.Close();
                //关闭连接
                client.Close();
            }

            catch (Exception e)
            {
                //异常处理
                SocketException se  = (SocketException)e.InnerException.GetBaseException();
                string          msg = string.Empty;
                switch (se.ErrorCode)
                {
                case 10060:
                    msg = string.Format(" 连接<font color=blue>控制系统 <b>{0}</b></font>处理超时",
                                        server);
                    break;

                case 10061:
                    msg = string.Format(" 连接<font color=blue>控制系统 <b>{0}</b></font> 超时,可能原因是机器或打料系统未启动",
                                        server);
                    break;

                case 10065:
                    msg = string.Format(" 无法连接<font color=blue>控制系统 <b>{0}</b></font>,可能网线没有插好,请检查网络连接!",
                                        server);
                    break;

                default:
                    msg = se.Message;
                    break;
                }
                log.Error(msg);
                throw new Exception(msg);
            }
            return(new
            {
                Status = status,
                ResultCode = resultCode,
                Message = message
            });
        }