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

 

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

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值