WEB开发网
开发学院软件开发C语言 P2P网络通讯程序(c#) 阅读

P2P网络通讯程序(c#)

 2009-03-27 08:22:11 来源:WEB开发网   
核心提示: b、MassTextPacket.cs --分片传输类[Serializable()] public class MassTextPacket:TextPacket { private int seqID; /// <summary> /// 包序列 /// </summ

b、MassTextPacket.cs  --分片传输类

 [Serializable()]
    public class MassTextPacket:TextPacket
    {
        private int seqID;
        /// <summary>
        /// 包序列
        /// </summary>
        public int SeqID
        {
            get { return seqID; }
            set { seqID = value; }
        }

        private int seqCount;
        /// <summary>
        /// 包数量
        /// </summary>
        public int SeqCount
        {
            get { return seqCount; }
            set { seqCount = value; }
        }

        private int _CLSD;
        public int CLSD
        {
            get { return _CLSD; }
            set { _CLSD = value; }
        }

    }

2、客户端

a、消息传送时进行p2p通讯

        private bool SendMessageTo(string toUserName, Packet packet)
        {
            PeerEntity toUser = userList.Single(c => c.UserName == toUserName);
            if (toUser == null)
            {
                return false;
            }
            ReceivedACK = false;

            for (int i=0; i<MAXRETRY; i++)           
            {     
                // 如果对方P2P地址不为0,就试图以它为目的地址发送数据,
                // 如果发送失败,则认为此P2P地址无效
                if (toUser.P2PAddress != null && toUser.P2PAddress.Port != 0)
                {
                    if (packet.GetType() == typeof(TextPacket))
                    {
                        TextPacket msgPacket = new TextPacket(toUserName, (packet as TextPacket).Message);
                        byte[] buffer = UtilityHelper.Serialize(msgPacket);
                        if (buffer.Length > MAXBUFFERSIZE)
                        {
                        
                            MassTextPacket mtp = new MassTextPacket();
                            mtp.SeqID = 0;
                            mtp.SeqCount = (int)System.Math.Ceiling(buffer.Length / (decimal)MAXBUFFERSIZE);
                            mtp.CLSD = mtp.GetHashCode();
                           
                            long pos = 0;
                            long count = buffer.Length < MAXBUFFERSIZE ? buffer.Length : MAXBUFFERSIZE;
                            while (pos < buffer.Length && pos > 0)
                            {
                                byte[] bytes = new byte[count];                          ;
                                for (int k = 0; k < count; k++)
                                    bytes[k] = buffer[pos + k];
                                //数据组包
                                mtp.SeqID = mtp.SeqID + 1;
                                mtp.Message = Convert.ToBase64String(bytes);

                                //发送数据
                                byte[] buf = UtilityHelper.Serialize(mtp);
                                client.Send(buf, buf.Length, toUser.P2PAddress);
                                Thread.Sleep(100);
                            }
                        }
                        else
                            client.Send(buffer, buffer.Length, toUser.P2PAddress);
                    }
                    else if (packet.GetType() == typeof(FileStreamPacket))
                    {
                        FileStreamPacket fsp = packet as FileStreamPacket;
                        System.IO.FileStream fs = new System.IO.FileStream(fsp.FileName, System.IO.FileMode.Open, System.IO.FileAccess.Read, FileShare.Read);

                       handle1.Reset();

                        fsp.SeqID = 0;
                        fsp.SeqCount = (int)System.Math.Ceiling(fs.Length / (decimal)MAXBUFFERSIZE);
                        fsp.CLSD = fsp.GetHashCode();

                        long pos = 0;
                        long count = fs.Length < MAXBUFFERSIZE ? fs.Length : MAXBUFFERSIZE;
                        while (pos < fs.Length && count > 0)
                        {
                            byte[] buffer = new byte[count];
                            fs.Seek(pos, SeekOrigin.Begin);
                            fs.Read(buffer, 0, (int)count);

                            pos += count;
                            count = pos + MAXBUFFERSIZE < fs.Length ? MAXBUFFERSIZE : fs.Length - pos;

                            //数据组包
                            fsp.SeqID = fsp.SeqID + 1;
                            fsp.Message = Convert.ToBase64String(buffer);

                            //发送数据
                            byte[] buf = UtilityHelper.Serialize(fsp);
                            client.Send(buf, buf.Length, toUser.P2PAddress);
                            Thread.Sleep(300);
                        }

                        handle1.Set();
                    }

                    // 等待接收线程将标记修改
                    for (int j = 0; j < 10; j++)
                    {
                        if (this.ReceivedACK)
                        {
                            this.ReceivedACK = false;
                            return true;
                        }
                        else
                        {
                            Thread.Sleep(300);
                        }
                    }
                }

                // 构建P2P打洞封包
                // 然后通过服务器转发,请求对方向自己打洞
                P2PConnectionPacket transMsg = new P2PConnectionPacket(UserName, toUserName);
                byte[] msgBuffer = UtilityHelper.Serialize(transMsg);
                client.Send(msgBuffer, msgBuffer.Length, hostPoint);

                // 等待对方的P2PCONNECTACK消息
                for(int j = 0; j < 10; ++j)
                {
                    toUser = userList.Single(c => c.UserName == toUserName);
                    if ( toUser.P2PAddress != null && toUser.P2PAddress.Port != 0)
                        break;
                    Thread.Sleep(300);
                }
               
            }
            return false;
        }

上一页  1 2 3 4 5 6  下一页

Tags:PP 网络通讯 程序

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接