Socket发送、接受数据并解析数据

it2022-05-08  11

 

using System;using System.Collections.Generic;using System.Text;using System.Net;using System.Net.Sockets;using System.Configuration;using System.Xml;using System.Collections.Specialized;using System.IO;

namespace abc{    public class Socket    {        static string ip ="";        static int port = 80;        static Socket socket;

        /// <summary>        /// 连接Socket服务器        /// </summary>        static void ConnectSocket()        {            try            {                //IPAddress ipAddress = IPAddress.Parse(ip);                //IPEndPoint iep = new IPEndPoint(ipAddress,port);

                socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);                socket.Connect(ip,port);            }            catch (SocketException ex)            {                throw new SocketException(ex.ErrorCode);            }        }

        /// <summary>        /// 发送接受数据        /// </summary>        /// <param name="info">发送的数据</param>        /// <returns></returns>        static byte[] SendAndReceive(string info)        {            ConnectSocket();            byte[] sendMsg = Encoding.GetEncoding("GBK").GetBytes(info);            socket.Send(sendMsg);//发送数据

            byte[] receiveByte=new byte[1024];            string receiveMsg = "";            int count = 0;            do            {                count = socket.Receive(receiveByte, receiveByte.Length, 0);//接受数据                receiveMsg += Encoding.GetEncoding("GBK").GetString(receiveByte,0,count);//转换为GBK格式的字符串            }            while (count > 0);            receiveMsg = receiveMsg.Replace("\n\r", "");            byte[] receive = Encoding.GetEncoding("GBK").GetBytes(receiveMsg);            socket.Shutdown(SocketShutdown.Both);//禁止发送和接受数据            socket.Close();//关闭连接释放资源            return receive;        }

        public static NameValueCollection getApplyPolicy(string xmlInfo)        {            byte[] by_receive = SendAndReceive(xmlInfo);

            string[] paths ={ "/Response/Head", "/Response/Content" };            return Resolves(by_receive, paths);        }

        /// <summary>        /// 解析返回数据        /// </summary>        /// <param name="by_receiveXml">返回的XML数据</param>        /// <param name="xPaths"></param>        /// <returns></returns>        static NameValueCollection Resolves(byte[] by_receiveXml, params string[] xPaths)        {            XmlDocument xdoc = new XmlDocument();            //结果hash            xdoc.LoadXml(Encoding.Default.GetString(by_receiveXml));            NameValueCollection items = new NameValueCollection();            foreach (string xPath in xPaths)            {                XmlNodeList xnl = xdoc.SelectSingleNode(xPath).ChildNodes;                foreach (XmlNode xd in xnl)                {                    if (xd.ChildNodes.Count > 1)                    {                        XmlNodeList xnl1 = xd.ChildNodes;                        string values = "";                        foreach (XmlNode xd1 in xnl1)                        {                            values += "," + xd1.InnerText;                        }                        if (values.Trim() != "")                        {                            values = values.Remove(0, 1);                        }                        items.Add(xd.Name, values);                    }                    else                    {                        items.Add(xd.Name, xd.InnerText);                    }                }            }            return items;        }    }}

 

转载于:https://www.cnblogs.com/Happy-learning/archive/2012/05/18/2507603.html

相关资源:Socket发送并接收服务器返回的数据

最新回复(0)