[转]使用C#.NET 实现高性能IPX/SPX SOCKET服务器 附源码

要实现 IPX/SPX 必须自己写IPX地址类

它派生于 EndPoint. 因为.NET没有提供此类所以必须自己写

    public class IPXEndPoint : EndPoint
    {
        byte[] NetNum;
        byte[] NodeNum;
        ushort Prot;



        public IPXEndPoint(ushort prot)
        {
            NetNum = new byte[4];
            NodeNum = new byte[6];
            Prot = prot;
        }

        public IPXEndPoint(string node, int num, ushort prot)
        {
            byte[] nodex = new byte[6];

            string[] nodesp = node.Split(new char[] { '-' }, StringSplitOptions.RemoveEmptyEntries);

            if (nodesp.Length == 6)
            {
                for(int i=0;i<6;i++)
                {
                    nodex[i] = Convert.ToByte("0x"+nodesp[i],16);
                    
                }

                this.NodeNum = nodex;

                this.NetNum = new byte[4];

                NetNum[3] = (byte)(num >> 24);
                NetNum[2] = (byte)(num >> 16);
                NetNum[1] = (byte)(num >> 8);
                NetNum[0] = (byte)(num);

                this.Prot = prot;

            }
            else
            {
                throw new ArgumentException("node");
            }

        }


        public IPXEndPoint(byte[] netnum, byte[] nodenum, ushort prot)
        {
            this.NetNum = netnum;
            this.NodeNum = nodenum;
            this.Prot = prot;
        }

        public override System.Net.Sockets.AddressFamily AddressFamily
        {
            get
            {
                return System.Net.Sockets.AddressFamily.Ipx;
            }
        }

        public override EndPoint Create(SocketAddress socketAddress)
        {
            if (socketAddress == null)
            {
                throw new ArgumentNullException("socketAddress");
            }
            if (socketAddress.Family != this.AddressFamily)
            {
                throw new ArgumentException("socketAddress");
            }
            if (socketAddress.Size < 14)
            {
                throw new ArgumentException("socketAddress");
            }

            ushort port = (ushort)(((socketAddress[12] << 8) & 0xff00) | socketAddress[13]);

           //return new IPEndPoint(((((socketAddress[4] & 0xff) | ((socketAddress[5] << 8) & 0xff00)) | ((socketAddress[6] << 0x10) & 0xff0000)) | (socketAddress[7] << 0x18)) & ((long)0xffffffffL), port);

            byte[] netnum = new byte[4];

            netnum[3] = socketAddress[2];
            netnum[2] = socketAddress[3];
            netnum[1] = socketAddress[4];
            netnum[0] = socketAddress[5];


            byte[] codenum = new byte[6];

            codenum[0] = socketAddress[6];
            codenum[1] = socketAddress[7];
            codenum[2] = socketAddress[8];
            codenum[3] = socketAddress[9];
            codenum[4] = socketAddress[10];
            codenum[5] = socketAddress[11];

            return new IPXEndPoint(netnum, codenum, port);        
                
        }


        public override SocketAddress Serialize()
        {
            SocketAddress address = new SocketAddress(System.Net.Sockets.AddressFamily.Ipx, 14);
            address[2]=NetNum[3];
            address[3] = NetNum[2];
            address[4] = NetNum[1];
            address[5] = NetNum[0];
            address[6] = NodeNum[0];
            address[7] = NodeNum[1];
            address[8] = NodeNum[2];
            address[9] = NodeNum[3];
            address[10] = NodeNum[4];
            address[11] = NodeNum[5];
            address[12] = (byte)(Prot >> 8);
            address[13] = (byte)Prot;

            return address;

        }

        public override string ToString()
        {
            int num = BitConverter.ToInt32(NetNum,0);
         
            string node = "";

            for(int i=0;i<NodeNum.Length;i++)
            {
                node += Convert.ToString(NodeNum[i], 16);
                if (i < NodeNum.Length - 1)
                {
                    node += "-";
                }
            }

            return node + ":0x" + Convert.ToString(num,16) + ":" + Prot;
            
        }
    }

 

 

写好后 初始化SOCKET对象:

       sock = new Socket(AddressFamily.Ipx, SocketType.Stream, ProtocolType.Spx);

注意 Addressfamily和protype

 

初始化地址

IPXEndPoint myEnd = new IPXEndPoint("MAC地址", 节点号, 端口);

sock.Bind(myEnd);

其他的和常规的TCP服务一样..

你可以到我的个人代码上传空间去下载 基于 ZYSocketSuper 扩展的超级 SOCKET IPX/SPX服务器端 模型

 

博客地址:http://blog.csdn.net/luyikk/article/details/5185534

 

posted @ 2015-01-10 15:02  Net-Spider  阅读(527)  评论(0)    收藏  举报