Ejemplo n.º 1
0
        void SendJoinPacket()
        {
            UdpClient udpclient = new UdpClient(ServerPort);
            IPAddress multicastaddress = IPAddress.Parse(ServerIP);
            udpclient.JoinMulticastGroup(multicastaddress);
            IPEndPoint remoteep = new IPEndPoint(multicastaddress, ServerPort);

            while (true)
            {
                if (lstSendBuf.Count == 0) Thread.Sleep(1);
                else
                {
                    byte[] Buffer = null;
                    lock (lstSendBuf)
                    {
                        Buffer = lstSendBuf[0];
                        lstSendBuf.RemoveAt(0);
                    }

                    int FullSize = Buffer.Length;
                    byte NumPacket = (byte)(FullSize / MaxSizeBuf);
                    if (FullSize % MaxSizeBuf != 0)
                    {
                        NumPacket++;
                    }
                    byte CurPacket = 0;
                    int SendSize = 0;
                    MainIndexFrame++;

                    while (FullSize != 0)
                    {
                        if (FullSize > MaxSizeBuf)
                            SendSize = MaxSizeBuf;
                        else SendSize = FullSize;

                        var up = new UDP_Packet
                        {
                            Data = new byte[SendSize],
                            Index = MainIndexFrame,
                            NumPacket = NumPacket,
                            CurPacket = (CurPacket++)
                        };

                        Array.Copy(Buffer, Buffer.Length - FullSize, up.Data, 0, SendSize);

                        byte[] bSend = up.ToByte();
                        SendNumBytes += bSend.Length;
                        BytesDataRateSend += bSend.Length;
                        NumSendPacket++;
                        //      client_socket.BeginSendTo(bSend, 0, bSend.Length, SocketFlags.None,ipend, CallBackSend, client_socket);
                      //  client_socket.SendTo(bSend, ipend);
                        udpclient.Send(bSend, bSend.Length, remoteep);
                        FullSize -= SendSize;
                    }

                    if ((DateTime.Now - dt_dateRate).TotalMilliseconds >= 1000)
                    {
                        dt_dateRate = DateTime.Now;
                        BytesInSec = BytesDataRateSend;
                        BytesDataRateSend = 0;
                    }

                    if (lstSendBuf.Count > 1000)
                    {

                    }
                }
            }
        }
Ejemplo n.º 2
0
        //*
        private void SendBuf(byte[] Buffer)
        {
            if (Buffer == null) return;
            // Creation Socket
            Socket client_socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

            // Conncet with the server
            IPEndPoint ipend = new IPEndPoint(IPAddress.Parse(ServerIP), ServerPort);

            // Send the Text
            int FullSize = Buffer.Length;
            byte NumPacket = (byte)(FullSize/MaxSizeBuf);
            if(FullSize%MaxSizeBuf!=0)
            {
                NumPacket++;
            }
            byte CurPacket = 0;
            int SendSize = 0;
            MainIndexFrame++;

            while (FullSize!=0)
            {
                if (FullSize > MaxSizeBuf)
                    SendSize = MaxSizeBuf;
                else SendSize = FullSize;

                var up = new UDP_Packet
                                    {
                                        Data = new byte[SendSize],
                                        Index = MainIndexFrame,
                                        NumPacket = NumPacket,
                                        CurPacket = (CurPacket++)
                                    };

                Array.Copy(Buffer, Buffer.Length - FullSize, up.Data, 0, SendSize);

                byte[] bSend = up.ToByte();
                SendNumBytes += bSend.Length;
                BytesDataRateSend += bSend.Length;
                NumSendPacket++;
              //      client_socket.BeginSendTo(bSend, 0, bSend.Length, SocketFlags.None,ipend, CallBackSend, client_socket);
                client_socket.SendTo(bSend, ipend);
                FullSize -= SendSize;
            }

            // Conncetion Close
            client_socket.Close();

            if((DateTime.Now - dt_dateRate).TotalMilliseconds>=1000)
            {
                dt_dateRate = DateTime.Now;
                BytesInSec = BytesDataRateSend;
                BytesDataRateSend = 0;
            }
        }